<html><head><meta content="text/html; charset=UTF-8" http-equiv="content-type"><style type="text/css">@import url('https://themes.googleusercontent.com/fonts/css?kit=cepHBTf3izbVXyASkFL14lwAMUXCyKvCMIUNm9th4Xc');.lst-kix_dsyvh3rdzgxo-1>li:before{content:"" counter(lst-ctn-kix_dsyvh3rdzgxo-1,lower-latin) ". "}.lst-kix_dsyvh3rdzgxo-3>li:before{content:"" counter(lst-ctn-kix_dsyvh3rdzgxo-3,decimal) ". "}.lst-kix_dsyvh3rdzgxo-0>li:before{content:"" counter(lst-ctn-kix_dsyvh3rdzgxo-0,decimal) ". "}.lst-kix_dsyvh3rdzgxo-4>li:before{content:"" counter(lst-ctn-kix_dsyvh3rdzgxo-4,lower-latin) ". "}.lst-kix_dsyvh3rdzgxo-5>li:before{content:"" counter(lst-ctn-kix_dsyvh3rdzgxo-5,lower-roman) ". "}ol.lst-kix_diq9v8p1rag-5.start{counter-reset:lst-ctn-kix_diq9v8p1rag-5 0}.lst-kix_1zujz7wm8wi3-6>li{counter-increment:lst-ctn-kix_1zujz7wm8wi3-6}.lst-kix_dsyvh3rdzgxo-6>li:before{content:"" counter(lst-ctn-kix_dsyvh3rdzgxo-6,decimal) ". "}.lst-kix_k4ut5gcvp390-3>li{counter-increment:lst-ctn-kix_k4ut5gcvp390-3}.lst-kix_dsyvh3rdzgxo-7>li:before{content:"" counter(lst-ctn-kix_dsyvh3rdzgxo-7,lower-latin) ". "}.lst-kix_dsyvh3rdzgxo-8>li:before{content:"" counter(lst-ctn-kix_dsyvh3rdzgxo-8,lower-roman) ". "}ol.lst-kix_x8hveywbunjm-5.start{counter-reset:lst-ctn-kix_x8hveywbunjm-5 0}.lst-kix_gm333outdumx-7>li{counter-increment:lst-ctn-kix_gm333outdumx-7}.lst-kix_257b3v5lp885-3>li:before{content:"\0025cf  "}ol.lst-kix_mf9a5h7facs9-3{list-style-type:none}ol.lst-kix_mf9a5h7facs9-2{list-style-type:none}.lst-kix_257b3v5lp885-4>li:before{content:"\0025cb  "}ol.lst-kix_mf9a5h7facs9-1{list-style-type:none}ol.lst-kix_mf9a5h7facs9-0{list-style-type:none}.lst-kix_257b3v5lp885-5>li:before{content:"\0025a0  "}ol.lst-kix_mf9a5h7facs9-7{list-style-type:none}ol.lst-kix_mf9a5h7facs9-6{list-style-type:none}ol.lst-kix_mf9a5h7facs9-5{list-style-type:none}ol.lst-kix_mf9a5h7facs9-4{list-style-type:none}.lst-kix_257b3v5lp885-6>li:before{content:"\0025cf  "}.lst-kix_diq9v8p1rag-7>li{counter-increment:lst-ctn-kix_diq9v8p1rag-7}ol.lst-kix_gm333outdumx-3.start{counter-reset:lst-ctn-kix_gm333outdumx-3 0}ol.lst-kix_mf9a5h7facs9-8{list-style-type:none}.lst-kix_257b3v5lp885-7>li:before{content:"\0025cb  "}.lst-kix_257b3v5lp885-8>li:before{content:"\0025a0  "}ol.lst-kix_dsyvh3rdzgxo-0.start{counter-reset:lst-ctn-kix_dsyvh3rdzgxo-0 0}ol.lst-kix_q0c03zlevnjq-2.start{counter-reset:lst-ctn-kix_q0c03zlevnjq-2 0}.lst-kix_gm333outdumx-3>li{counter-increment:lst-ctn-kix_gm333outdumx-3}.lst-kix_mf9a5h7facs9-1>li:before{content:"" counter(lst-ctn-kix_mf9a5h7facs9-1,lower-latin) ". "}.lst-kix_dsyvh3rdzgxo-2>li{counter-increment:lst-ctn-kix_dsyvh3rdzgxo-2}ol.lst-kix_unuqtgbahdmw-4.start{counter-reset:lst-ctn-kix_unuqtgbahdmw-4 0}.lst-kix_mf9a5h7facs9-0>li:before{content:"" counter(lst-ctn-kix_mf9a5h7facs9-0,decimal) ". "}.lst-kix_x8hveywbunjm-7>li{counter-increment:lst-ctn-kix_x8hveywbunjm-7}.lst-kix_gbfbgvqhgeod-5>li{counter-increment:lst-ctn-kix_gbfbgvqhgeod-5}ol.lst-kix_x8hveywbunjm-0.start{counter-reset:lst-ctn-kix_x8hveywbunjm-0 0}.lst-kix_t5715nnbapc2-3>li{counter-increment:lst-ctn-kix_t5715nnbapc2-3}ol.lst-kix_t5715nnbapc2-4.start{counter-reset:lst-ctn-kix_t5715nnbapc2-4 0}.lst-kix_diq9v8p1rag-6>li:before{content:"" counter(lst-ctn-kix_diq9v8p1rag-6,decimal) ". "}.lst-kix_diq9v8p1rag-0>li{counter-increment:lst-ctn-kix_diq9v8p1rag-0}.lst-kix_diq9v8p1rag-7>li:before{content:"" counter(lst-ctn-kix_diq9v8p1rag-7,lower-latin) ". "}.lst-kix_diq9v8p1rag-8>li:before{content:"" counter(lst-ctn-kix_diq9v8p1rag-8,lower-roman) ". "}.lst-kix_257b3v5lp885-2>li:before{content:"\0025a0  "}ul.lst-kix_xsbmx9pyg7e2-0{list-style-type:none}.lst-kix_257b3v5lp885-1>li:before{content:"\0025cb  "}ul.lst-kix_u63z0wo5str6-0{list-style-type:none}ul.lst-kix_xsbmx9pyg7e2-1{list-style-type:none}.lst-kix_x8hveywbunjm-0>li{counter-increment:lst-ctn-kix_x8hveywbunjm-0}ul.lst-kix_u63z0wo5str6-1{list-style-type:none}ul.lst-kix_xsbmx9pyg7e2-2{list-style-type:none}ul.lst-kix_u63z0wo5str6-2{list-style-type:none}ul.lst-kix_xsbmx9pyg7e2-3{list-style-type:none}ul.lst-kix_u63z0wo5str6-3{list-style-type:none}ul.lst-kix_u63z0wo5str6-4{list-style-type:none}ul.lst-kix_u63z0wo5str6-5{list-style-type:none}.lst-kix_257b3v5lp885-0>li:before{content:"\0025cf  "}ol.lst-kix_1zujz7wm8wi3-4.start{counter-reset:lst-ctn-kix_1zujz7wm8wi3-4 0}ul.lst-kix_u63z0wo5str6-6{list-style-type:none}ol.lst-kix_gm333outdumx-8.start{counter-reset:lst-ctn-kix_gm333outdumx-8 0}.lst-kix_9uwjp0g1busc-6>li{counter-increment:lst-ctn-kix_9uwjp0g1busc-6}.lst-kix_mf9a5h7facs9-0>li{counter-increment:lst-ctn-kix_mf9a5h7facs9-0}ol.lst-kix_9uwjp0g1busc-5.start{counter-reset:lst-ctn-kix_9uwjp0g1busc-5 0}ul.lst-kix_xsbmx9pyg7e2-8{list-style-type:none}.lst-kix_1zujz7wm8wi3-2>li{counter-increment:lst-ctn-kix_1zujz7wm8wi3-2}ul.lst-kix_xsbmx9pyg7e2-4{list-style-type:none}ul.lst-kix_xsbmx9pyg7e2-5{list-style-type:none}ul.lst-kix_xsbmx9pyg7e2-6{list-style-type:none}.lst-kix_dsyvh3rdzgxo-2>li:before{content:"" counter(lst-ctn-kix_dsyvh3rdzgxo-2,lower-roman) ". "}ul.lst-kix_xsbmx9pyg7e2-7{list-style-type:none}.lst-kix_9uwjp0g1busc-3>li{counter-increment:lst-ctn-kix_9uwjp0g1busc-3}ol.lst-kix_k4ut5gcvp390-4.start{counter-reset:lst-ctn-kix_k4ut5gcvp390-4 0}.lst-kix_gm333outdumx-0>li{counter-increment:lst-ctn-kix_gm333outdumx-0}.lst-kix_x8hveywbunjm-4>li{counter-increment:lst-ctn-kix_x8hveywbunjm-4}.lst-kix_gbfbgvqhgeod-2>li{counter-increment:lst-ctn-kix_gbfbgvqhgeod-2}.lst-kix_7lx1v6khmzxb-0>li:before{content:"\0025cf  "}ol.lst-kix_mf9a5h7facs9-2.start{counter-reset:lst-ctn-kix_mf9a5h7facs9-2 0}.lst-kix_unuqtgbahdmw-2>li{counter-increment:lst-ctn-kix_unuqtgbahdmw-2}.lst-kix_diq9v8p1rag-2>li:before{content:"" counter(lst-ctn-kix_diq9v8p1rag-2,lower-roman) ". "}ol.lst-kix_9uwjp0g1busc-3.start{counter-reset:lst-ctn-kix_9uwjp0g1busc-3 0}.lst-kix_xsbmx9pyg7e2-6>li:before{content:"\0025cf  "}.lst-kix_t5715nnbapc2-6>li{counter-increment:lst-ctn-kix_t5715nnbapc2-6}.lst-kix_mf9a5h7facs9-7>li{counter-increment:lst-ctn-kix_mf9a5h7facs9-7}ol.lst-kix_gbfbgvqhgeod-2.start{counter-reset:lst-ctn-kix_gbfbgvqhgeod-2 0}.lst-kix_diq9v8p1rag-4>li:before{content:"" counter(lst-ctn-kix_diq9v8p1rag-4,lower-latin) ". "}.lst-kix_xsbmx9pyg7e2-8>li:before{content:"\0025a0  "}ol.lst-kix_unuqtgbahdmw-2{list-style-type:none}ol.lst-kix_unuqtgbahdmw-3{list-style-type:none}ol.lst-kix_unuqtgbahdmw-4{list-style-type:none}.lst-kix_diq9v8p1rag-4>li{counter-increment:lst-ctn-kix_diq9v8p1rag-4}ol.lst-kix_unuqtgbahdmw-5{list-style-type:none}ol.lst-kix_unuqtgbahdmw-6{list-style-type:none}ol.lst-kix_9uwjp0g1busc-0.start{counter-reset:lst-ctn-kix_9uwjp0g1busc-0 0}ol.lst-kix_diq9v8p1rag-3.start{counter-reset:lst-ctn-kix_diq9v8p1rag-3 0}ol.lst-kix_unuqtgbahdmw-7{list-style-type:none}ol.lst-kix_unuqtgbahdmw-8{list-style-type:none}.lst-kix_mf9a5h7facs9-4>li:before{content:"" counter(lst-ctn-kix_mf9a5h7facs9-4,lower-latin) ". "}.lst-kix_diq9v8p1rag-0>li:before{content:"" counter(lst-ctn-kix_diq9v8p1rag-0,decimal) ". "}ol.lst-kix_unuqtgbahdmw-0{list-style-type:none}.lst-kix_mf9a5h7facs9-2>li:before{content:"" counter(lst-ctn-kix_mf9a5h7facs9-2,lower-roman) ". "}ol.lst-kix_unuqtgbahdmw-1{list-style-type:none}.lst-kix_q0c03zlevnjq-2>li{counter-increment:lst-ctn-kix_q0c03zlevnjq-2}.lst-kix_1zujz7wm8wi3-3>li:before{content:"" counter(lst-ctn-kix_1zujz7wm8wi3-3,decimal) ". "}.lst-kix_1zujz7wm8wi3-7>li:before{content:"" counter(lst-ctn-kix_1zujz7wm8wi3-7,lower-latin) ". "}.lst-kix_mf9a5h7facs9-8>li{counter-increment:lst-ctn-kix_mf9a5h7facs9-8}ol.lst-kix_mf9a5h7facs9-4.start{counter-reset:lst-ctn-kix_mf9a5h7facs9-4 0}ol.lst-kix_k4ut5gcvp390-2.start{counter-reset:lst-ctn-kix_k4ut5gcvp390-2 0}.lst-kix_1zujz7wm8wi3-1>li:before{content:"" counter(lst-ctn-kix_1zujz7wm8wi3-1,lower-latin) ". "}.lst-kix_mf9a5h7facs9-6>li:before{content:"" counter(lst-ctn-kix_mf9a5h7facs9-6,decimal) ". "}ol.lst-kix_diq9v8p1rag-0.start{counter-reset:lst-ctn-kix_diq9v8p1rag-0 0}ol.lst-kix_dsyvh3rdzgxo-8{list-style-type:none}ol.lst-kix_1zujz7wm8wi3-7.start{counter-reset:lst-ctn-kix_1zujz7wm8wi3-7 0}ol.lst-kix_dsyvh3rdzgxo-6{list-style-type:none}ol.lst-kix_t5715nnbapc2-0{list-style-type:none}ol.lst-kix_dsyvh3rdzgxo-7{list-style-type:none}ol.lst-kix_dsyvh3rdzgxo-4{list-style-type:none}ol.lst-kix_dsyvh3rdzgxo-5{list-style-type:none}ol.lst-kix_dsyvh3rdzgxo-2{list-style-type:none}.lst-kix_mf9a5h7facs9-8>li:before{content:"" counter(lst-ctn-kix_mf9a5h7facs9-8,lower-roman) ". "}ol.lst-kix_dsyvh3rdzgxo-3{list-style-type:none}ol.lst-kix_dsyvh3rdzgxo-0{list-style-type:none}ol.lst-kix_t5715nnbapc2-7.start{counter-reset:lst-ctn-kix_t5715nnbapc2-7 0}ol.lst-kix_dsyvh3rdzgxo-1{list-style-type:none}.lst-kix_1zujz7wm8wi3-5>li:before{content:"" counter(lst-ctn-kix_1zujz7wm8wi3-5,lower-roman) ". "}.lst-kix_xsbmx9pyg7e2-4>li:before{content:"\0025cb  "}ol.lst-kix_gm333outdumx-5.start{counter-reset:lst-ctn-kix_gm333outdumx-5 0}.lst-kix_xsbmx9pyg7e2-2>li:before{content:"\0025a0  "}ol.lst-kix_t5715nnbapc2-6{list-style-type:none}ol.lst-kix_t5715nnbapc2-5{list-style-type:none}ol.lst-kix_t5715nnbapc2-8{list-style-type:none}ol.lst-kix_t5715nnbapc2-7{list-style-type:none}ol.lst-kix_t5715nnbapc2-2{list-style-type:none}ol.lst-kix_t5715nnbapc2-1{list-style-type:none}ol.lst-kix_x8hveywbunjm-3.start{counter-reset:lst-ctn-kix_x8hveywbunjm-3 0}.lst-kix_7lx1v6khmzxb-8>li:before{content:"\0025a0  "}ol.lst-kix_t5715nnbapc2-4{list-style-type:none}.lst-kix_xsbmx9pyg7e2-0>li:before{content:"\0025cf  "}ol.lst-kix_t5715nnbapc2-3{list-style-type:none}ol.lst-kix_unuqtgbahdmw-1.start{counter-reset:lst-ctn-kix_unuqtgbahdmw-1 0}.lst-kix_7lx1v6khmzxb-2>li:before{content:"\0025a0  "}.lst-kix_7lx1v6khmzxb-6>li:before{content:"\0025cf  "}ol.lst-kix_q0c03zlevnjq-7.start{counter-reset:lst-ctn-kix_q0c03zlevnjq-7 0}ol.lst-kix_gbfbgvqhgeod-0.start{counter-reset:lst-ctn-kix_gbfbgvqhgeod-0 0}.lst-kix_7lx1v6khmzxb-4>li:before{content:"\0025cb  "}ol.lst-kix_9uwjp0g1busc-2.start{counter-reset:lst-ctn-kix_9uwjp0g1busc-2 0}.lst-kix_gm333outdumx-2>li:before{content:"" counter(lst-ctn-kix_gm333outdumx-2,lower-roman) ". "}.lst-kix_gm333outdumx-6>li:before{content:"" counter(lst-ctn-kix_gm333outdumx-6,decimal) ". "}ol.lst-kix_gm333outdumx-6.start{counter-reset:lst-ctn-kix_gm333outdumx-6 0}.lst-kix_gm333outdumx-1>li:before{content:"" counter(lst-ctn-kix_gm333outdumx-1,lower-latin) ". "}.lst-kix_gm333outdumx-5>li:before{content:"" counter(lst-ctn-kix_gm333outdumx-5,lower-roman) ". "}ol.lst-kix_dsyvh3rdzgxo-3.start{counter-reset:lst-ctn-kix_dsyvh3rdzgxo-3 0}.lst-kix_gm333outdumx-6>li{counter-increment:lst-ctn-kix_gm333outdumx-6}.lst-kix_b10rbaxir35-2>li:before{content:"\0025a0  "}.lst-kix_1zujz7wm8wi3-7>li{counter-increment:lst-ctn-kix_1zujz7wm8wi3-7}.lst-kix_t5715nnbapc2-0>li{counter-increment:lst-ctn-kix_t5715nnbapc2-0}.lst-kix_unuqtgbahdmw-3>li:before{content:"" counter(lst-ctn-kix_unuqtgbahdmw-3,decimal) ". "}.lst-kix_unuqtgbahdmw-4>li:before{content:"" counter(lst-ctn-kix_unuqtgbahdmw-4,lower-latin) ". "}ol.lst-kix_diq9v8p1rag-2.start{counter-reset:lst-ctn-kix_diq9v8p1rag-2 0}ol.lst-kix_gbfbgvqhgeod-4.start{counter-reset:lst-ctn-kix_gbfbgvqhgeod-4 0}.lst-kix_9uwjp0g1busc-7>li:before{content:"" counter(lst-ctn-kix_9uwjp0g1busc-7,lower-latin) ". "}.lst-kix_unuqtgbahdmw-7>li:before{content:"" counter(lst-ctn-kix_unuqtgbahdmw-7,lower-latin) ". "}.lst-kix_unuqtgbahdmw-8>li:before{content:"" counter(lst-ctn-kix_unuqtgbahdmw-8,lower-roman) ". "}ol.lst-kix_x8hveywbunjm-8.start{counter-reset:lst-ctn-kix_x8hveywbunjm-8 0}.lst-kix_9uwjp0g1busc-3>li:before{content:"" counter(lst-ctn-kix_9uwjp0g1busc-3,decimal) ". "}.lst-kix_9uwjp0g1busc-2>li:before{content:"" counter(lst-ctn-kix_9uwjp0g1busc-2,lower-roman) ". "}ol.lst-kix_k4ut5gcvp390-6.start{counter-reset:lst-ctn-kix_k4ut5gcvp390-6 0}ol.lst-kix_gm333outdumx-4{list-style-type:none}ol.lst-kix_gm333outdumx-5{list-style-type:none}ol.lst-kix_gm333outdumx-2{list-style-type:none}ol.lst-kix_gm333outdumx-3{list-style-type:none}.lst-kix_b10rbaxir35-3>li:before{content:"\0025cf  "}ol.lst-kix_gm333outdumx-8{list-style-type:none}ol.lst-kix_gm333outdumx-6{list-style-type:none}ol.lst-kix_gm333outdumx-7{list-style-type:none}.lst-kix_b10rbaxir35-7>li:before{content:"\0025cb  "}.lst-kix_9uwjp0g1busc-6>li:before{content:"" counter(lst-ctn-kix_9uwjp0g1busc-6,decimal) ". "}ol.lst-kix_gm333outdumx-0{list-style-type:none}ol.lst-kix_gm333outdumx-1{list-style-type:none}ol.lst-kix_mf9a5h7facs9-6.start{counter-reset:lst-ctn-kix_mf9a5h7facs9-6 0}.lst-kix_b10rbaxir35-6>li:before{content:"\0025cf  "}ol.lst-kix_1zujz7wm8wi3-6{list-style-type:none}ol.lst-kix_1zujz7wm8wi3-5{list-style-type:none}ol.lst-kix_1zujz7wm8wi3-8{list-style-type:none}ol.lst-kix_1zujz7wm8wi3-7{list-style-type:none}ul.lst-kix_uzlm6n9ijqls-5{list-style-type:none}ul.lst-kix_uzlm6n9ijqls-6{list-style-type:none}ul.lst-kix_uzlm6n9ijqls-7{list-style-type:none}ol.lst-kix_q0c03zlevnjq-5.start{counter-reset:lst-ctn-kix_q0c03zlevnjq-5 0}ul.lst-kix_uzlm6n9ijqls-8{list-style-type:none}ol.lst-kix_1zujz7wm8wi3-0{list-style-type:none}ol.lst-kix_1zujz7wm8wi3-2{list-style-type:none}ol.lst-kix_1zujz7wm8wi3-1{list-style-type:none}ol.lst-kix_x8hveywbunjm-7.start{counter-reset:lst-ctn-kix_x8hveywbunjm-7 0}ol.lst-kix_1zujz7wm8wi3-4{list-style-type:none}ol.lst-kix_1zujz7wm8wi3-3{list-style-type:none}.lst-kix_k4ut5gcvp390-6>li{counter-increment:lst-ctn-kix_k4ut5gcvp390-6}.lst-kix_q0c03zlevnjq-5>li:before{content:"" counter(lst-ctn-kix_q0c03zlevnjq-5,lower-roman) ". "}.lst-kix_t5715nnbapc2-7>li{counter-increment:lst-ctn-kix_t5715nnbapc2-7}ol.lst-kix_mf9a5h7facs9-7.start{counter-reset:lst-ctn-kix_mf9a5h7facs9-7 0}ol.lst-kix_k4ut5gcvp390-5.start{counter-reset:lst-ctn-kix_k4ut5gcvp390-5 0}.lst-kix_q0c03zlevnjq-8>li:before{content:"" counter(lst-ctn-kix_q0c03zlevnjq-8,lower-roman) ". "}ol.lst-kix_q0c03zlevnjq-4.start{counter-reset:lst-ctn-kix_q0c03zlevnjq-4 0}.lst-kix_q0c03zlevnjq-4>li:before{content:"" counter(lst-ctn-kix_q0c03zlevnjq-4,lower-latin) ". "}ol.lst-kix_dsyvh3rdzgxo-2.start{counter-reset:lst-ctn-kix_dsyvh3rdzgxo-2 0}ol.lst-kix_dsyvh3rdzgxo-8.start{counter-reset:lst-ctn-kix_dsyvh3rdzgxo-8 0}.lst-kix_mf9a5h7facs9-3>li{counter-increment:lst-ctn-kix_mf9a5h7facs9-3}.lst-kix_unuqtgbahdmw-0>li:before{content:"" counter(lst-ctn-kix_unuqtgbahdmw-0,decimal) ". "}ul.lst-kix_uzlm6n9ijqls-1{list-style-type:none}ol.lst-kix_gbfbgvqhgeod-3.start{counter-reset:lst-ctn-kix_gbfbgvqhgeod-3 0}ul.lst-kix_uzlm6n9ijqls-2{list-style-type:none}ul.lst-kix_uzlm6n9ijqls-3{list-style-type:none}.lst-kix_9uwjp0g1busc-0>li{counter-increment:lst-ctn-kix_9uwjp0g1busc-0}.lst-kix_q0c03zlevnjq-1>li:before{content:"" counter(lst-ctn-kix_q0c03zlevnjq-1,lower-latin) ". "}ul.lst-kix_uzlm6n9ijqls-4{list-style-type:none}.lst-kix_q0c03zlevnjq-0>li:before{content:"" counter(lst-ctn-kix_q0c03zlevnjq-0,decimal) ". "}ul.lst-kix_uzlm6n9ijqls-0{list-style-type:none}.lst-kix_dsyvh3rdzgxo-5>li{counter-increment:lst-ctn-kix_dsyvh3rdzgxo-5}ol.lst-kix_k4ut5gcvp390-7.start{counter-reset:lst-ctn-kix_k4ut5gcvp390-7 0}.lst-kix_diq9v8p1rag-2>li{counter-increment:lst-ctn-kix_diq9v8p1rag-2}ol.lst-kix_q0c03zlevnjq-0.start{counter-reset:lst-ctn-kix_q0c03zlevnjq-0 0}ol.lst-kix_gbfbgvqhgeod-5.start{counter-reset:lst-ctn-kix_gbfbgvqhgeod-5 0}.lst-kix_q0c03zlevnjq-5>li{counter-increment:lst-ctn-kix_q0c03zlevnjq-5}ol.lst-kix_dsyvh3rdzgxo-7.start{counter-reset:lst-ctn-kix_dsyvh3rdzgxo-7 0}.lst-kix_mf9a5h7facs9-5>li{counter-increment:lst-ctn-kix_mf9a5h7facs9-5}.lst-kix_7lx1v6khmzxb-1>li:before{content:"\0025cb  "}ol.lst-kix_x8hveywbunjm-8{list-style-type:none}ol.lst-kix_x8hveywbunjm-7{list-style-type:none}ol.lst-kix_x8hveywbunjm-6{list-style-type:none}ol.lst-kix_q0c03zlevnjq-3.start{counter-reset:lst-ctn-kix_q0c03zlevnjq-3 0}.lst-kix_201raoy85h6y-5>li:before{content:"-  "}.lst-kix_u63z0wo5str6-3>li:before{content:"\0025cf  "}ul.lst-kix_u63z0wo5str6-7{list-style-type:none}ol.lst-kix_x8hveywbunjm-1{list-style-type:none}ul.lst-kix_u63z0wo5str6-8{list-style-type:none}ol.lst-kix_x8hveywbunjm-0{list-style-type:none}.lst-kix_t5715nnbapc2-6>li:before{content:"" counter(lst-ctn-kix_t5715nnbapc2-6,decimal) ". "}ol.lst-kix_x8hveywbunjm-5{list-style-type:none}ol.lst-kix_x8hveywbunjm-4{list-style-type:none}ol.lst-kix_x8hveywbunjm-3{list-style-type:none}ol.lst-kix_x8hveywbunjm-2{list-style-type:none}.lst-kix_t5715nnbapc2-2>li:before{content:"" counter(lst-ctn-kix_t5715nnbapc2-2,lower-roman) ". "}.lst-kix_xsbmx9pyg7e2-7>li:before{content:"\0025cb  "}.lst-kix_k4ut5gcvp390-2>li:before{content:"" counter(lst-ctn-kix_k4ut5gcvp390-2,lower-roman) ". "}ol.lst-kix_diq9v8p1rag-6.start{counter-reset:lst-ctn-kix_diq9v8p1rag-6 0}.lst-kix_201raoy85h6y-1>li:before{content:"-  "}ol.lst-kix_dsyvh3rdzgxo-4.start{counter-reset:lst-ctn-kix_dsyvh3rdzgxo-4 0}.lst-kix_9uwjp0g1busc-2>li{counter-increment:lst-ctn-kix_9uwjp0g1busc-2}.lst-kix_diq9v8p1rag-3>li:before{content:"" counter(lst-ctn-kix_diq9v8p1rag-3,decimal) ". "}ol.lst-kix_9uwjp0g1busc-6.start{counter-reset:lst-ctn-kix_9uwjp0g1busc-6 0}.lst-kix_gbfbgvqhgeod-3>li:before{content:"" counter(lst-ctn-kix_gbfbgvqhgeod-3,decimal) ". "}.lst-kix_u63z0wo5str6-7>li:before{content:"\0025cb  "}.lst-kix_gbfbgvqhgeod-7>li:before{content:"" counter(lst-ctn-kix_gbfbgvqhgeod-7,lower-latin) ". "}.lst-kix_mf9a5h7facs9-3>li:before{content:"" counter(lst-ctn-kix_mf9a5h7facs9-3,decimal) ". "}ol.lst-kix_gbfbgvqhgeod-8.start{counter-reset:lst-ctn-kix_gbfbgvqhgeod-8 0}ol.lst-kix_dsyvh3rdzgxo-5.start{counter-reset:lst-ctn-kix_dsyvh3rdzgxo-5 0}.lst-kix_t5715nnbapc2-2>li{counter-increment:lst-ctn-kix_t5715nnbapc2-2}ol.lst-kix_diq9v8p1rag-7.start{counter-reset:lst-ctn-kix_diq9v8p1rag-7 0}.lst-kix_unuqtgbahdmw-0>li{counter-increment:lst-ctn-kix_unuqtgbahdmw-0}.lst-kix_1zujz7wm8wi3-2>li:before{content:"" counter(lst-ctn-kix_1zujz7wm8wi3-2,lower-roman) ". "}.lst-kix_1zujz7wm8wi3-6>li:before{content:"" counter(lst-ctn-kix_1zujz7wm8wi3-6,decimal) ". "}.lst-kix_k4ut5gcvp390-1>li{counter-increment:lst-ctn-kix_k4ut5gcvp390-1}.lst-kix_9uwjp0g1busc-7>li{counter-increment:lst-ctn-kix_9uwjp0g1busc-7}.lst-kix_1zujz7wm8wi3-4>li{counter-increment:lst-ctn-kix_1zujz7wm8wi3-4}.lst-kix_unuqtgbahdmw-5>li{counter-increment:lst-ctn-kix_unuqtgbahdmw-5}.lst-kix_mf9a5h7facs9-7>li:before{content:"" counter(lst-ctn-kix_mf9a5h7facs9-7,lower-latin) ". "}ol.lst-kix_gbfbgvqhgeod-7.start{counter-reset:lst-ctn-kix_gbfbgvqhgeod-7 0}.lst-kix_gm333outdumx-4>li{counter-increment:lst-ctn-kix_gm333outdumx-4}ol.lst-kix_9uwjp0g1busc-7.start{counter-reset:lst-ctn-kix_9uwjp0g1busc-7 0}ol.lst-kix_q0c03zlevnjq-1.start{counter-reset:lst-ctn-kix_q0c03zlevnjq-1 0}.lst-kix_xsbmx9pyg7e2-3>li:before{content:"\0025cf  "}.lst-kix_k4ut5gcvp390-6>li:before{content:"" counter(lst-ctn-kix_k4ut5gcvp390-6,decimal) ". "}ol.lst-kix_k4ut5gcvp390-8.start{counter-reset:lst-ctn-kix_k4ut5gcvp390-8 0}.lst-kix_k4ut5gcvp390-8>li{counter-increment:lst-ctn-kix_k4ut5gcvp390-8}.lst-kix_gbfbgvqhgeod-0>li{counter-increment:lst-ctn-kix_gbfbgvqhgeod-0}ol.lst-kix_diq9v8p1rag-8.start{counter-reset:lst-ctn-kix_diq9v8p1rag-8 0}ol.lst-kix_gbfbgvqhgeod-6.start{counter-reset:lst-ctn-kix_gbfbgvqhgeod-6 0}.lst-kix_dsyvh3rdzgxo-0>li{counter-increment:lst-ctn-kix_dsyvh3rdzgxo-0}.lst-kix_x8hveywbunjm-2>li{counter-increment:lst-ctn-kix_x8hveywbunjm-2}ol.lst-kix_diq9v8p1rag-0{list-style-type:none}ol.lst-kix_diq9v8p1rag-2{list-style-type:none}ol.lst-kix_diq9v8p1rag-1{list-style-type:none}ol.lst-kix_diq9v8p1rag-4{list-style-type:none}ol.lst-kix_diq9v8p1rag-3{list-style-type:none}ol.lst-kix_diq9v8p1rag-6{list-style-type:none}ol.lst-kix_diq9v8p1rag-5{list-style-type:none}.lst-kix_7lx1v6khmzxb-5>li:before{content:"\0025a0  "}ol.lst-kix_diq9v8p1rag-8{list-style-type:none}ol.lst-kix_diq9v8p1rag-7{list-style-type:none}ol.lst-kix_9uwjp0g1busc-8.start{counter-reset:lst-ctn-kix_9uwjp0g1busc-8 0}.lst-kix_uzlm6n9ijqls-4>li:before{content:"\0025cb  "}.lst-kix_gbfbgvqhgeod-7>li{counter-increment:lst-ctn-kix_gbfbgvqhgeod-7}ol.lst-kix_gbfbgvqhgeod-1.start{counter-reset:lst-ctn-kix_gbfbgvqhgeod-1 0}ul.lst-kix_b10rbaxir35-6{list-style-type:none}.lst-kix_uzlm6n9ijqls-3>li:before{content:"\0025cf  "}ul.lst-kix_b10rbaxir35-7{list-style-type:none}ul.lst-kix_b10rbaxir35-8{list-style-type:none}ol.lst-kix_k4ut5gcvp390-3.start{counter-reset:lst-ctn-kix_k4ut5gcvp390-3 0}.lst-kix_uzlm6n9ijqls-2>li:before{content:"\0025a0  "}.lst-kix_uzlm6n9ijqls-1>li:before{content:"\0025cb  "}ol.lst-kix_unuqtgbahdmw-5.start{counter-reset:lst-ctn-kix_unuqtgbahdmw-5 0}.lst-kix_uzlm6n9ijqls-0>li:before{content:"\0025cf  "}.lst-kix_x8hveywbunjm-7>li:before{content:"" counter(lst-ctn-kix_x8hveywbunjm-7,lower-latin) ". "}.lst-kix_x8hveywbunjm-6>li:before{content:"" counter(lst-ctn-kix_x8hveywbunjm-6,decimal) ". "}ol.lst-kix_dsyvh3rdzgxo-6.start{counter-reset:lst-ctn-kix_dsyvh3rdzgxo-6 0}.lst-kix_x8hveywbunjm-5>li:before{content:"" counter(lst-ctn-kix_x8hveywbunjm-5,lower-roman) ". "}ul.lst-kix_b10rbaxir35-2{list-style-type:none}ul.lst-kix_b10rbaxir35-3{list-style-type:none}ul.lst-kix_b10rbaxir35-4{list-style-type:none}.lst-kix_x8hveywbunjm-2>li:before{content:"" counter(lst-ctn-kix_x8hveywbunjm-2,lower-roman) ". "}.lst-kix_x8hveywbunjm-4>li:before{content:"" counter(lst-ctn-kix_x8hveywbunjm-4,lower-latin) ". "}ul.lst-kix_b10rbaxir35-5{list-style-type:none}ol.lst-kix_mf9a5h7facs9-3.start{counter-reset:lst-ctn-kix_mf9a5h7facs9-3 0}.lst-kix_q0c03zlevnjq-0>li{counter-increment:lst-ctn-kix_q0c03zlevnjq-0}ul.lst-kix_b10rbaxir35-0{list-style-type:none}.lst-kix_t5715nnbapc2-1>li{counter-increment:lst-ctn-kix_t5715nnbapc2-1}.lst-kix_x8hveywbunjm-3>li:before{content:"" counter(lst-ctn-kix_x8hveywbunjm-3,decimal) ". "}ul.lst-kix_b10rbaxir35-1{list-style-type:none}.lst-kix_x8hveywbunjm-0>li:before{content:"" counter(lst-ctn-kix_x8hveywbunjm-0,decimal) ". "}.lst-kix_gm333outdumx-5>li{counter-increment:lst-ctn-kix_gm333outdumx-5}.lst-kix_x8hveywbunjm-1>li:before{content:"" counter(lst-ctn-kix_x8hveywbunjm-1,lower-latin) ". "}ol.lst-kix_t5715nnbapc2-3.start{counter-reset:lst-ctn-kix_t5715nnbapc2-3 0}ol.lst-kix_1zujz7wm8wi3-3.start{counter-reset:lst-ctn-kix_1zujz7wm8wi3-3 0}ul.lst-kix_257b3v5lp885-3{list-style-type:none}ul.lst-kix_257b3v5lp885-2{list-style-type:none}ul.lst-kix_257b3v5lp885-1{list-style-type:none}ul.lst-kix_257b3v5lp885-0{list-style-type:none}ol.lst-kix_mf9a5h7facs9-8.start{counter-reset:lst-ctn-kix_mf9a5h7facs9-8 0}ul.lst-kix_257b3v5lp885-7{list-style-type:none}.lst-kix_q0c03zlevnjq-7>li{counter-increment:lst-ctn-kix_q0c03zlevnjq-7}ul.lst-kix_257b3v5lp885-6{list-style-type:none}ul.lst-kix_257b3v5lp885-5{list-style-type:none}ol.lst-kix_diq9v8p1rag-4.start{counter-reset:lst-ctn-kix_diq9v8p1rag-4 0}ul.lst-kix_257b3v5lp885-4{list-style-type:none}.lst-kix_dsyvh3rdzgxo-7>li{counter-increment:lst-ctn-kix_dsyvh3rdzgxo-7}ul.lst-kix_257b3v5lp885-8{list-style-type:none}ol.lst-kix_9uwjp0g1busc-4.start{counter-reset:lst-ctn-kix_9uwjp0g1busc-4 0}ol.lst-kix_dsyvh3rdzgxo-1.start{counter-reset:lst-ctn-kix_dsyvh3rdzgxo-1 0}ul.lst-kix_201raoy85h6y-8{list-style-type:none}.lst-kix_x8hveywbunjm-8>li:before{content:"" counter(lst-ctn-kix_x8hveywbunjm-8,lower-roman) ". "}ul.lst-kix_201raoy85h6y-7{list-style-type:none}.lst-kix_k99j9p95k9dh-8>li:before{content:"\0025a0  "}.lst-kix_unuqtgbahdmw-7>li{counter-increment:lst-ctn-kix_unuqtgbahdmw-7}ul.lst-kix_201raoy85h6y-2{list-style-type:none}.lst-kix_1zujz7wm8wi3-8>li{counter-increment:lst-ctn-kix_1zujz7wm8wi3-8}.lst-kix_k99j9p95k9dh-5>li:before{content:"\0025a0  "}.lst-kix_k99j9p95k9dh-6>li:before{content:"\0025cf  "}ul.lst-kix_201raoy85h6y-1{list-style-type:none}ul.lst-kix_201raoy85h6y-0{list-style-type:none}ul.lst-kix_201raoy85h6y-6{list-style-type:none}.lst-kix_k99j9p95k9dh-7>li:before{content:"\0025cb  "}ul.lst-kix_201raoy85h6y-5{list-style-type:none}ul.lst-kix_201raoy85h6y-4{list-style-type:none}ul.lst-kix_201raoy85h6y-3{list-style-type:none}ol.lst-kix_gm333outdumx-2.start{counter-reset:lst-ctn-kix_gm333outdumx-2 0}.lst-kix_uzlm6n9ijqls-5>li:before{content:"\0025a0  "}.lst-kix_k99j9p95k9dh-1>li:before{content:"\0025cb  "}.lst-kix_k99j9p95k9dh-2>li:before{content:"\0025a0  "}.lst-kix_uzlm6n9ijqls-7>li:before{content:"\0025cb  "}ol.lst-kix_x8hveywbunjm-6.start{counter-reset:lst-ctn-kix_x8hveywbunjm-6 0}.lst-kix_k99j9p95k9dh-0>li:before{content:"\0025cf  "}.lst-kix_k99j9p95k9dh-4>li:before{content:"\0025cb  "}.lst-kix_uzlm6n9ijqls-6>li:before{content:"\0025cf  "}.lst-kix_k99j9p95k9dh-3>li:before{content:"\0025cf  "}.lst-kix_uzlm6n9ijqls-8>li:before{content:"\0025a0  "}.lst-kix_t5715nnbapc2-5>li{counter-increment:lst-ctn-kix_t5715nnbapc2-5}.lst-kix_9uwjp0g1busc-4>li{counter-increment:lst-ctn-kix_9uwjp0g1busc-4}.lst-kix_unuqtgbahdmw-3>li{counter-increment:lst-ctn-kix_unuqtgbahdmw-3}.lst-kix_1zujz7wm8wi3-0>li{counter-increment:lst-ctn-kix_1zujz7wm8wi3-0}ol.lst-kix_gbfbgvqhgeod-1{list-style-type:none}ol.lst-kix_gbfbgvqhgeod-2{list-style-type:none}ol.lst-kix_1zujz7wm8wi3-8.start{counter-reset:lst-ctn-kix_1zujz7wm8wi3-8 0}ol.lst-kix_gbfbgvqhgeod-0{list-style-type:none}ol.lst-kix_gbfbgvqhgeod-5{list-style-type:none}ol.lst-kix_gbfbgvqhgeod-6{list-style-type:none}ol.lst-kix_gbfbgvqhgeod-3{list-style-type:none}ol.lst-kix_gbfbgvqhgeod-4{list-style-type:none}.lst-kix_201raoy85h6y-8>li:before{content:"-  "}.lst-kix_1zujz7wm8wi3-1>li{counter-increment:lst-ctn-kix_1zujz7wm8wi3-1}ol.lst-kix_gbfbgvqhgeod-7{list-style-type:none}.lst-kix_u63z0wo5str6-4>li:before{content:"\0025cb  "}ol.lst-kix_gbfbgvqhgeod-8{list-style-type:none}.lst-kix_t5715nnbapc2-4>li{counter-increment:lst-ctn-kix_t5715nnbapc2-4}.lst-kix_t5715nnbapc2-7>li:before{content:"" counter(lst-ctn-kix_t5715nnbapc2-7,lower-latin) ". "}.lst-kix_9uwjp0g1busc-5>li{counter-increment:lst-ctn-kix_9uwjp0g1busc-5}.lst-kix_t5715nnbapc2-5>li:before{content:"" counter(lst-ctn-kix_t5715nnbapc2-5,lower-roman) ". "}.lst-kix_u63z0wo5str6-0>li:before{content:"\0025cf  "}.lst-kix_u63z0wo5str6-2>li:before{content:"\0025a0  "}.lst-kix_dsyvh3rdzgxo-4>li{counter-increment:lst-ctn-kix_dsyvh3rdzgxo-4}.lst-kix_201raoy85h6y-6>li:before{content:"-  "}ol.lst-kix_1zujz7wm8wi3-5.start{counter-reset:lst-ctn-kix_1zujz7wm8wi3-5 0}ol.lst-kix_q0c03zlevnjq-6.start{counter-reset:lst-ctn-kix_q0c03zlevnjq-6 0}.lst-kix_201raoy85h6y-0>li:before{content:"-  "}.lst-kix_t5715nnbapc2-1>li:before{content:"" counter(lst-ctn-kix_t5715nnbapc2-1,lower-latin) ". "}.lst-kix_x8hveywbunjm-6>li{counter-increment:lst-ctn-kix_x8hveywbunjm-6}.lst-kix_t5715nnbapc2-3>li:before{content:"" counter(lst-ctn-kix_t5715nnbapc2-3,decimal) ". "}.lst-kix_k4ut5gcvp390-3>li:before{content:"" counter(lst-ctn-kix_k4ut5gcvp390-3,decimal) ". "}.lst-kix_gbfbgvqhgeod-0>li:before{content:"" counter(lst-ctn-kix_gbfbgvqhgeod-0,decimal) ". "}.lst-kix_201raoy85h6y-4>li:before{content:"-  "}ol.lst-kix_gm333outdumx-7.start{counter-reset:lst-ctn-kix_gm333outdumx-7 0}ol.lst-kix_unuqtgbahdmw-3.start{counter-reset:lst-ctn-kix_unuqtgbahdmw-3 0}.lst-kix_201raoy85h6y-2>li:before{content:"-  "}.lst-kix_gbfbgvqhgeod-2>li:before{content:"" counter(lst-ctn-kix_gbfbgvqhgeod-2,lower-roman) ". "}ol.lst-kix_q0c03zlevnjq-8.start{counter-reset:lst-ctn-kix_q0c03zlevnjq-8 0}.lst-kix_k4ut5gcvp390-1>li:before{content:"" counter(lst-ctn-kix_k4ut5gcvp390-1,lower-latin) ". "}.lst-kix_q0c03zlevnjq-3>li{counter-increment:lst-ctn-kix_q0c03zlevnjq-3}.lst-kix_gbfbgvqhgeod-4>li:before{content:"" counter(lst-ctn-kix_gbfbgvqhgeod-4,lower-latin) ". "}.lst-kix_u63z0wo5str6-6>li:before{content:"\0025cf  "}.lst-kix_u63z0wo5str6-8>li:before{content:"\0025a0  "}ol.lst-kix_x8hveywbunjm-1.start{counter-reset:lst-ctn-kix_x8hveywbunjm-1 0}.lst-kix_gbfbgvqhgeod-6>li:before{content:"" counter(lst-ctn-kix_gbfbgvqhgeod-6,decimal) ". "}.lst-kix_gm333outdumx-2>li{counter-increment:lst-ctn-kix_gm333outdumx-2}ol.lst-kix_gm333outdumx-4.start{counter-reset:lst-ctn-kix_gm333outdumx-4 0}ol.lst-kix_x8hveywbunjm-4.start{counter-reset:lst-ctn-kix_x8hveywbunjm-4 0}.lst-kix_diq9v8p1rag-5>li{counter-increment:lst-ctn-kix_diq9v8p1rag-5}.lst-kix_mf9a5h7facs9-2>li{counter-increment:lst-ctn-kix_mf9a5h7facs9-2}.lst-kix_q0c03zlevnjq-8>li{counter-increment:lst-ctn-kix_q0c03zlevnjq-8}ol.lst-kix_q0c03zlevnjq-4{list-style-type:none}ol.lst-kix_q0c03zlevnjq-5{list-style-type:none}ol.lst-kix_q0c03zlevnjq-6{list-style-type:none}ol.lst-kix_q0c03zlevnjq-7{list-style-type:none}ol.lst-kix_q0c03zlevnjq-8{list-style-type:none}.lst-kix_k4ut5gcvp390-7>li:before{content:"" counter(lst-ctn-kix_k4ut5gcvp390-7,lower-latin) ". "}ol.lst-kix_q0c03zlevnjq-0{list-style-type:none}ol.lst-kix_q0c03zlevnjq-1{list-style-type:none}ol.lst-kix_q0c03zlevnjq-2{list-style-type:none}ol.lst-kix_unuqtgbahdmw-0.start{counter-reset:lst-ctn-kix_unuqtgbahdmw-0 0}.lst-kix_k4ut5gcvp390-5>li:before{content:"" counter(lst-ctn-kix_k4ut5gcvp390-5,lower-roman) ". "}ol.lst-kix_q0c03zlevnjq-3{list-style-type:none}ol.lst-kix_mf9a5h7facs9-5.start{counter-reset:lst-ctn-kix_mf9a5h7facs9-5 0}ol.lst-kix_k4ut5gcvp390-1.start{counter-reset:lst-ctn-kix_k4ut5gcvp390-1 0}ul.lst-kix_k99j9p95k9dh-0{list-style-type:none}ul.lst-kix_k99j9p95k9dh-1{list-style-type:none}ul.lst-kix_k99j9p95k9dh-2{list-style-type:none}ul.lst-kix_k99j9p95k9dh-3{list-style-type:none}ol.lst-kix_diq9v8p1rag-1.start{counter-reset:lst-ctn-kix_diq9v8p1rag-1 0}ul.lst-kix_k99j9p95k9dh-4{list-style-type:none}.lst-kix_k4ut5gcvp390-5>li{counter-increment:lst-ctn-kix_k4ut5gcvp390-5}ol.lst-kix_9uwjp0g1busc-1.start{counter-reset:lst-ctn-kix_9uwjp0g1busc-1 0}ul.lst-kix_k99j9p95k9dh-5{list-style-type:none}ul.lst-kix_k99j9p95k9dh-6{list-style-type:none}ul.lst-kix_k99j9p95k9dh-7{list-style-type:none}ul.lst-kix_k99j9p95k9dh-8{list-style-type:none}.lst-kix_gm333outdumx-1>li{counter-increment:lst-ctn-kix_gm333outdumx-1}ul.lst-kix_7lx1v6khmzxb-6{list-style-type:none}ul.lst-kix_7lx1v6khmzxb-5{list-style-type:none}.lst-kix_x8hveywbunjm-5>li{counter-increment:lst-ctn-kix_x8hveywbunjm-5}ul.lst-kix_7lx1v6khmzxb-4{list-style-type:none}ul.lst-kix_7lx1v6khmzxb-3{list-style-type:none}ol.lst-kix_t5715nnbapc2-8.start{counter-reset:lst-ctn-kix_t5715nnbapc2-8 0}ul.lst-kix_7lx1v6khmzxb-2{list-style-type:none}ul.lst-kix_7lx1v6khmzxb-1{list-style-type:none}ul.lst-kix_7lx1v6khmzxb-0{list-style-type:none}.lst-kix_dsyvh3rdzgxo-3>li{counter-increment:lst-ctn-kix_dsyvh3rdzgxo-3}.lst-kix_gbfbgvqhgeod-3>li{counter-increment:lst-ctn-kix_gbfbgvqhgeod-3}ul.lst-kix_7lx1v6khmzxb-8{list-style-type:none}ul.lst-kix_7lx1v6khmzxb-7{list-style-type:none}.lst-kix_gm333outdumx-4>li:before{content:"" counter(lst-ctn-kix_gm333outdumx-4,lower-latin) ". "}ol.lst-kix_t5715nnbapc2-6.start{counter-reset:lst-ctn-kix_t5715nnbapc2-6 0}.lst-kix_gbfbgvqhgeod-8>li{counter-increment:lst-ctn-kix_gbfbgvqhgeod-8}.lst-kix_b10rbaxir35-8>li:before{content:"\0025a0  "}ol.lst-kix_x8hveywbunjm-2.start{counter-reset:lst-ctn-kix_x8hveywbunjm-2 0}.lst-kix_gm333outdumx-0>li:before{content:"" counter(lst-ctn-kix_gm333outdumx-0,decimal) ". "}.lst-kix_gm333outdumx-8>li:before{content:"" counter(lst-ctn-kix_gm333outdumx-8,lower-roman) ". "}ol.lst-kix_unuqtgbahdmw-2.start{counter-reset:lst-ctn-kix_unuqtgbahdmw-2 0}.lst-kix_gm333outdumx-7>li:before{content:"" counter(lst-ctn-kix_gm333outdumx-7,lower-latin) ". "}.lst-kix_unuqtgbahdmw-8>li{counter-increment:lst-ctn-kix_unuqtgbahdmw-8}.lst-kix_b10rbaxir35-1>li:before{content:"\0025cb  "}.lst-kix_b10rbaxir35-0>li:before{content:"\0025cf  "}.lst-kix_unuqtgbahdmw-5>li:before{content:"" counter(lst-ctn-kix_unuqtgbahdmw-5,lower-roman) ". "}ol.lst-kix_k4ut5gcvp390-0.start{counter-reset:lst-ctn-kix_k4ut5gcvp390-0 0}.lst-kix_gm333outdumx-3>li:before{content:"" counter(lst-ctn-kix_gm333outdumx-3,decimal) ". "}.lst-kix_unuqtgbahdmw-6>li:before{content:"" counter(lst-ctn-kix_unuqtgbahdmw-6,decimal) ". "}.lst-kix_9uwjp0g1busc-8>li:before{content:"" counter(lst-ctn-kix_9uwjp0g1busc-8,lower-roman) ". "}.lst-kix_mf9a5h7facs9-1>li{counter-increment:lst-ctn-kix_mf9a5h7facs9-1}ol.lst-kix_gm333outdumx-0.start{counter-reset:lst-ctn-kix_gm333outdumx-0 0}ol.lst-kix_mf9a5h7facs9-0.start{counter-reset:lst-ctn-kix_mf9a5h7facs9-0 0}ol.lst-kix_t5715nnbapc2-0.start{counter-reset:lst-ctn-kix_t5715nnbapc2-0 0}ol.lst-kix_9uwjp0g1busc-5{list-style-type:none}ol.lst-kix_9uwjp0g1busc-6{list-style-type:none}ol.lst-kix_9uwjp0g1busc-3{list-style-type:none}ol.lst-kix_9uwjp0g1busc-4{list-style-type:none}ol.lst-kix_1zujz7wm8wi3-6.start{counter-reset:lst-ctn-kix_1zujz7wm8wi3-6 0}ol.lst-kix_9uwjp0g1busc-7{list-style-type:none}.lst-kix_gm333outdumx-8>li{counter-increment:lst-ctn-kix_gm333outdumx-8}ol.lst-kix_9uwjp0g1busc-8{list-style-type:none}.lst-kix_b10rbaxir35-4>li:before{content:"\0025cb  "}.lst-kix_9uwjp0g1busc-4>li:before{content:"" counter(lst-ctn-kix_9uwjp0g1busc-4,lower-latin) ". "}.lst-kix_9uwjp0g1busc-5>li:before{content:"" counter(lst-ctn-kix_9uwjp0g1busc-5,lower-roman) ". "}.lst-kix_b10rbaxir35-5>li:before{content:"\0025a0  "}.lst-kix_k4ut5gcvp390-4>li{counter-increment:lst-ctn-kix_k4ut5gcvp390-4}ol.lst-kix_9uwjp0g1busc-1{list-style-type:none}ol.lst-kix_9uwjp0g1busc-2{list-style-type:none}ol.lst-kix_9uwjp0g1busc-0{list-style-type:none}.lst-kix_dsyvh3rdzgxo-8>li{counter-increment:lst-ctn-kix_dsyvh3rdzgxo-8}.lst-kix_x8hveywbunjm-1>li{counter-increment:lst-ctn-kix_x8hveywbunjm-1}.lst-kix_gbfbgvqhgeod-8>li:before{content:"" counter(lst-ctn-kix_gbfbgvqhgeod-8,lower-roman) ". "}.lst-kix_9uwjp0g1busc-0>li:before{content:"" counter(lst-ctn-kix_9uwjp0g1busc-0,decimal) ". "}.lst-kix_gbfbgvqhgeod-4>li{counter-increment:lst-ctn-kix_gbfbgvqhgeod-4}.lst-kix_9uwjp0g1busc-1>li:before{content:"" counter(lst-ctn-kix_9uwjp0g1busc-1,lower-latin) ". "}.lst-kix_q0c03zlevnjq-1>li{counter-increment:lst-ctn-kix_q0c03zlevnjq-1}.lst-kix_q0c03zlevnjq-6>li:before{content:"" counter(lst-ctn-kix_q0c03zlevnjq-6,decimal) ". "}.lst-kix_diq9v8p1rag-6>li{counter-increment:lst-ctn-kix_diq9v8p1rag-6}.lst-kix_q0c03zlevnjq-7>li:before{content:"" counter(lst-ctn-kix_q0c03zlevnjq-7,lower-latin) ". "}ol.lst-kix_gm333outdumx-1.start{counter-reset:lst-ctn-kix_gm333outdumx-1 0}.lst-kix_dsyvh3rdzgxo-1>li{counter-increment:lst-ctn-kix_dsyvh3rdzgxo-1}.lst-kix_unuqtgbahdmw-1>li{counter-increment:lst-ctn-kix_unuqtgbahdmw-1}.lst-kix_unuqtgbahdmw-4>li{counter-increment:lst-ctn-kix_unuqtgbahdmw-4}ol.lst-kix_t5715nnbapc2-5.start{counter-reset:lst-ctn-kix_t5715nnbapc2-5 0}.lst-kix_unuqtgbahdmw-2>li:before{content:"" counter(lst-ctn-kix_unuqtgbahdmw-2,lower-roman) ". "}.lst-kix_k4ut5gcvp390-0>li{counter-increment:lst-ctn-kix_k4ut5gcvp390-0}.lst-kix_unuqtgbahdmw-1>li:before{content:"" counter(lst-ctn-kix_unuqtgbahdmw-1,lower-latin) ". "}ol.lst-kix_k4ut5gcvp390-8{list-style-type:none}.lst-kix_q0c03zlevnjq-3>li:before{content:"" counter(lst-ctn-kix_q0c03zlevnjq-3,decimal) ". "}.lst-kix_q0c03zlevnjq-2>li:before{content:"" counter(lst-ctn-kix_q0c03zlevnjq-2,lower-roman) ". "}ol.lst-kix_k4ut5gcvp390-1{list-style-type:none}ol.lst-kix_k4ut5gcvp390-0{list-style-type:none}.lst-kix_1zujz7wm8wi3-5>li{counter-increment:lst-ctn-kix_1zujz7wm8wi3-5}ol.lst-kix_k4ut5gcvp390-3{list-style-type:none}ol.lst-kix_k4ut5gcvp390-2{list-style-type:none}ol.lst-kix_k4ut5gcvp390-5{list-style-type:none}ol.lst-kix_mf9a5h7facs9-1.start{counter-reset:lst-ctn-kix_mf9a5h7facs9-1 0}ol.lst-kix_k4ut5gcvp390-4{list-style-type:none}ol.lst-kix_k4ut5gcvp390-7{list-style-type:none}.lst-kix_mf9a5h7facs9-6>li{counter-increment:lst-ctn-kix_mf9a5h7facs9-6}ol.lst-kix_k4ut5gcvp390-6{list-style-type:none}ol.lst-kix_unuqtgbahdmw-6.start{counter-reset:lst-ctn-kix_unuqtgbahdmw-6 0}.lst-kix_q0c03zlevnjq-4>li{counter-increment:lst-ctn-kix_q0c03zlevnjq-4}.lst-kix_t5715nnbapc2-8>li:before{content:"" counter(lst-ctn-kix_t5715nnbapc2-8,lower-roman) ". "}ol.lst-kix_t5715nnbapc2-2.start{counter-reset:lst-ctn-kix_t5715nnbapc2-2 0}.lst-kix_gbfbgvqhgeod-1>li{counter-increment:lst-ctn-kix_gbfbgvqhgeod-1}.lst-kix_201raoy85h6y-7>li:before{content:"-  "}.lst-kix_u63z0wo5str6-1>li:before{content:"\0025cb  "}.lst-kix_diq9v8p1rag-3>li{counter-increment:lst-ctn-kix_diq9v8p1rag-3}.lst-kix_diq9v8p1rag-1>li:before{content:"" counter(lst-ctn-kix_diq9v8p1rag-1,lower-latin) ". "}.lst-kix_x8hveywbunjm-3>li{counter-increment:lst-ctn-kix_x8hveywbunjm-3}.lst-kix_xsbmx9pyg7e2-5>li:before{content:"\0025a0  "}.lst-kix_k4ut5gcvp390-0>li:before{content:"" counter(lst-ctn-kix_k4ut5gcvp390-0,decimal) ". "}.lst-kix_t5715nnbapc2-0>li:before{content:"" counter(lst-ctn-kix_t5715nnbapc2-0,decimal) ". "}.lst-kix_t5715nnbapc2-4>li:before{content:"" counter(lst-ctn-kix_t5715nnbapc2-4,lower-latin) ". "}.lst-kix_mf9a5h7facs9-4>li{counter-increment:lst-ctn-kix_mf9a5h7facs9-4}ol.lst-kix_1zujz7wm8wi3-2.start{counter-reset:lst-ctn-kix_1zujz7wm8wi3-2 0}.lst-kix_diq9v8p1rag-5>li:before{content:"" counter(lst-ctn-kix_diq9v8p1rag-5,lower-roman) ". "}.lst-kix_gbfbgvqhgeod-1>li:before{content:"" counter(lst-ctn-kix_gbfbgvqhgeod-1,lower-latin) ". "}.lst-kix_q0c03zlevnjq-6>li{counter-increment:lst-ctn-kix_q0c03zlevnjq-6}.lst-kix_201raoy85h6y-3>li:before{content:"-  "}.lst-kix_u63z0wo5str6-5>li:before{content:"\0025a0  "}.lst-kix_diq9v8p1rag-1>li{counter-increment:lst-ctn-kix_diq9v8p1rag-1}.lst-kix_gbfbgvqhgeod-5>li:before{content:"" counter(lst-ctn-kix_gbfbgvqhgeod-5,lower-roman) ". "}.lst-kix_k4ut5gcvp390-7>li{counter-increment:lst-ctn-kix_k4ut5gcvp390-7}.lst-kix_1zujz7wm8wi3-4>li:before{content:"" counter(lst-ctn-kix_1zujz7wm8wi3-4,lower-latin) ". "}.lst-kix_t5715nnbapc2-8>li{counter-increment:lst-ctn-kix_t5715nnbapc2-8}.lst-kix_9uwjp0g1busc-1>li{counter-increment:lst-ctn-kix_9uwjp0g1busc-1}.lst-kix_1zujz7wm8wi3-0>li:before{content:"" counter(lst-ctn-kix_1zujz7wm8wi3-0,decimal) ". "}.lst-kix_1zujz7wm8wi3-8>li:before{content:"" counter(lst-ctn-kix_1zujz7wm8wi3-8,lower-roman) ". "}ol.lst-kix_t5715nnbapc2-1.start{counter-reset:lst-ctn-kix_t5715nnbapc2-1 0}.lst-kix_1zujz7wm8wi3-3>li{counter-increment:lst-ctn-kix_1zujz7wm8wi3-3}.lst-kix_mf9a5h7facs9-5>li:before{content:"" counter(lst-ctn-kix_mf9a5h7facs9-5,lower-roman) ". "}.lst-kix_unuqtgbahdmw-6>li{counter-increment:lst-ctn-kix_unuqtgbahdmw-6}.lst-kix_k4ut5gcvp390-8>li:before{content:"" counter(lst-ctn-kix_k4ut5gcvp390-8,lower-roman) ". "}ol.lst-kix_1zujz7wm8wi3-0.start{counter-reset:lst-ctn-kix_1zujz7wm8wi3-0 0}ol.lst-kix_unuqtgbahdmw-8.start{counter-reset:lst-ctn-kix_unuqtgbahdmw-8 0}.lst-kix_9uwjp0g1busc-8>li{counter-increment:lst-ctn-kix_9uwjp0g1busc-8}.lst-kix_k4ut5gcvp390-2>li{counter-increment:lst-ctn-kix_k4ut5gcvp390-2}.lst-kix_k4ut5gcvp390-4>li:before{content:"" counter(lst-ctn-kix_k4ut5gcvp390-4,lower-latin) ". "}.lst-kix_7lx1v6khmzxb-7>li:before{content:"\0025cb  "}.lst-kix_xsbmx9pyg7e2-1>li:before{content:"\0025cb  "}.lst-kix_x8hveywbunjm-8>li{counter-increment:lst-ctn-kix_x8hveywbunjm-8}.lst-kix_7lx1v6khmzxb-3>li:before{content:"\0025cf  "}ol.lst-kix_1zujz7wm8wi3-1.start{counter-reset:lst-ctn-kix_1zujz7wm8wi3-1 0}ol.lst-kix_unuqtgbahdmw-7.start{counter-reset:lst-ctn-kix_unuqtgbahdmw-7 0}.lst-kix_dsyvh3rdzgxo-6>li{counter-increment:lst-ctn-kix_dsyvh3rdzgxo-6}.lst-kix_diq9v8p1rag-8>li{counter-increment:lst-ctn-kix_diq9v8p1rag-8}.lst-kix_gbfbgvqhgeod-6>li{counter-increment:lst-ctn-kix_gbfbgvqhgeod-6}ol{margin:0;padding:0}table td,table th{padding:0}.c64{border-right-style:solid;padding:0pt 8pt 0pt 8pt;border-bottom-color:#000000;border-top-width:0pt;border-right-width:0pt;border-left-color:#eeeeee;vertical-align:top;border-right-color:#000000;border-left-width:1pt;border-top-style:solid;border-left-style:solid;border-bottom-width:0pt;width:184.5pt;border-top-color:#000000;border-bottom-style:solid}.c100{border-right-style:solid;padding:0pt 8pt 0pt 8pt;border-bottom-color:#eeeeee;border-top-width:0pt;border-right-width:1pt;border-left-color:#eeeeee;vertical-align:top;border-right-color:#eeeeee;border-left-width:0pt;border-top-style:solid;border-left-style:solid;border-bottom-width:0pt;width:140.2pt;border-top-color:#eeeeee;border-bottom-style:solid}.c52{border-right-style:solid;padding:5pt 5pt 5pt 5pt;border-bottom-color:#000000;border-top-width:0pt;border-right-width:0pt;border-left-color:#000000;vertical-align:middle;border-right-color:#000000;border-left-width:0pt;border-top-style:solid;border-left-style:solid;border-bottom-width:0pt;width:55.8pt;border-top-color:#000000;border-bottom-style:solid}.c85{border-right-style:solid;padding:5pt 5pt 5pt 5pt;border-bottom-color:#000000;border-top-width:0pt;border-right-width:0pt;border-left-color:#000000;vertical-align:top;border-right-color:#000000;border-left-width:0pt;border-top-style:solid;border-left-style:solid;border-bottom-width:0pt;width:184.5pt;border-top-color:#000000;border-bottom-style:solid}.c49{border-right-style:solid;padding:5pt 5pt 5pt 5pt;border-bottom-color:#000000;border-top-width:0pt;border-right-width:0pt;border-left-color:#000000;vertical-align:middle;border-right-color:#000000;border-left-width:0pt;border-top-style:solid;border-left-style:solid;border-bottom-width:0pt;width:58pt;border-top-color:#000000;border-bottom-style:solid}.c26{border-right-style:solid;padding:5pt 5pt 5pt 5pt;border-bottom-color:#000000;border-top-width:0pt;border-right-width:0pt;border-left-color:#000000;vertical-align:middle;border-right-color:#000000;border-left-width:0pt;border-top-style:solid;border-left-style:solid;border-bottom-width:0pt;width:166pt;border-top-color:#000000;border-bottom-style:solid}.c67{border-right-style:solid;padding:5pt 5pt 5pt 5pt;border-bottom-color:#000000;border-top-width:0pt;border-right-width:0pt;border-left-color:#000000;vertical-align:middle;border-right-color:#000000;border-left-width:0pt;border-top-style:solid;border-left-style:solid;border-bottom-width:0pt;width:82pt;border-top-color:#000000;border-bottom-style:solid}.c109{border-right-style:solid;padding:0pt 8pt 0pt 8pt;border-bottom-color:#eeeeee;border-top-width:0pt;border-right-width:0pt;border-left-color:#000000;vertical-align:top;border-right-color:#000000;border-left-width:0pt;border-top-style:solid;border-left-style:solid;border-bottom-width:0pt;width:140.2pt;border-top-color:#000000;border-bottom-style:solid}.c6{color:#000000;font-weight:normal;text-decoration:none;vertical-align:baseline;font-size:11pt;font-family:"Arial";font-style:normal}.c34{color:#000000;font-weight:normal;text-decoration:none;vertical-align:baseline;font-size:11pt;font-style:normal}.c68{font-weight:normal;text-decoration:none;vertical-align:baseline;font-size:10pt;font-family:"Arial";font-style:normal}.c60{color:#000000;text-decoration:none;vertical-align:baseline;font-size:11pt;font-style:normal}.c37{padding-top:0pt;padding-bottom:0pt;line-height:1.15;text-align:left}.c86{padding-top:0pt;padding-bottom:0pt;line-height:1.365;text-align:left}.c94{padding-top:16pt;padding-bottom:4pt;line-height:1.15;text-align:left}.c31{border-spacing:0;border-collapse:collapse;margin-right:auto}.c21{padding-top:0pt;padding-bottom:0pt;line-height:1.0;text-align:left}.c43{font-size:11pt;color:#000000;font-weight:normal}.c32{padding-left:0pt;padding-bottom:6pt;line-height:1.4285714285714286}.c46{font-size:10.5pt;font-family:"Consolas";color:#008000}.c104{padding-top:0pt;padding-bottom:0pt;line-height:1.5}.c0{orphans:2;text-indent:36pt;widows:2}.c27{background-color:#f4fbf4;color:#809980}.c1{orphans:2;widows:2}.c39{padding-bottom:8pt;line-height:1.0}.c98{padding-bottom:8pt;line-height:2.142857142857143}.c92{font-family:"Consolas";color:#0000ff}.c2{color:#a9b7c6;font-family:"Bodoni"}.c19{border:1px solid black;margin:5px}.c47{background-color:#f4fbf4;color:#131513}.c105{max-width:468pt;padding:72pt 72pt 72pt 72pt}.c78{font-family:"Consolas";color:#0c4b33}.c53{padding-bottom:11pt;line-height:1.4}.c59{background-color:#f4fbf4;color:#ad2bee}.c91{background-color:#fff9e3;font-size:11.5pt}.c18{color:inherit;text-decoration:inherit}.c76{background-color:#2b2b2b;font-size:9pt}.c99{padding-bottom:6pt;line-height:1.4285714285714286}.c45{padding:0;margin:0}.c17{color:#1155cc;text-decoration:underline}.c57{color:#00635d;text-decoration:underline}.c75{color:#9900ff}.c93{color:#bababa}.c29{color:#b200b2}.c11{font-size:12pt}.c36{color:#38761d}.c96{color:#351c75}.c7{color:#cc7832}.c14{margin-left:54pt}.c40{color:#a5c261}.c12{color:#008080}.c54{color:#bbb529}.c41{vertical-align:super}.c102{font-family:"Verdana"}.c72{vertical-align:sub}.c35{font-size:18pt}.c69{font-weight:normal}.c23{margin-left:72pt}.c101{font-size:14pt}.c15{font-style:italic}.c63{color:#aa4926}.c107{height:14pt}.c4{font-family:"Courier New"}.c13{page-break-after:avoid}.c88{color:#980000}.c103{color:#e69138}.c77{font-size:10.5pt}.c81{color:#434343}.c3{font-family:"Bodoni"}.c30{color:#629755}.c44{color:#c65d09}.c24{color:#4070a0}.c79{color:#242729}.c82{font-size:11pt}.c55{background-color:#fafafa}.c51{margin-left:18pt}.c71{font-size:40pt}.c90{text-align:right}.c66{color:#93c47d}.c9{height:11pt}.c48{color:#222222}.c61{color:#808080}.c70{color:#674ea7}.c62{color:#e8bf6a}.c25{color:#8888c6}.c50{color:#666666}.c10{margin-left:36pt}.c56{color:#181818}.c28{color:#a9b7c6}.c16{color:#313131}.c80{font-size:30pt}.c95{margin-left:108pt}.c8{color:#94558d}.c108{color:#0b5394}.c33{color:#333333}.c73{color:#ff0000}.c38{padding-left:0pt}.c58{color:#00ff00}.c87{color:#70a0d0}.c97{font-family:"Consolas"}.c42{color:#007020}.c5{font-weight:bold}.c65{height:0pt}.c74{color:#666600}.c106{color:#208050}.c83{font-size:36pt}.c20{color:#6897bb}.c22{background-color:#ffffff}.c84{color:#0e84b5}.c89{line-height:1.0636363636363637}.title{padding-top:0pt;color:#000000;font-size:26pt;padding-bottom:3pt;font-family:"Arial";line-height:1.15;page-break-after:avoid;orphans:2;widows:2;text-align:left}.subtitle{padding-top:0pt;color:#666666;font-size:15pt;padding-bottom:16pt;font-family:"Arial";line-height:1.15;page-break-after:avoid;orphans:2;widows:2;text-align:left}li{color:#000000;font-size:11pt;font-family:"Arial"}p{margin:0;color:#000000;font-size:11pt;font-family:"Arial"}h1{padding-top:20pt;color:#000000;font-size:48pt;padding-bottom:6pt;font-family:"Bodoni";line-height:1.15;page-break-after:avoid;orphans:2;widows:2;text-align:left}h2{padding-top:18pt;color:#0b5394;font-weight:bold;font-size:18pt;padding-bottom:6pt;font-family:"Arial";line-height:1.15;page-break-after:avoid;orphans:2;widows:2;text-align:left}h3{padding-top:16pt;color:#38761d;font-weight:bold;font-size:14pt;padding-bottom:4pt;font-family:"Arial";line-height:1.15;page-break-after:avoid;orphans:2;widows:2;text-align:left}h4{padding-top:14pt;color:#666666;font-size:12pt;padding-bottom:4pt;font-family:"Arial";line-height:1.15;page-break-after:avoid;orphans:2;widows:2;text-align:left}h5{padding-top:12pt;color:#666666;font-size:11pt;padding-bottom:4pt;font-family:"Arial";line-height:1.15;page-break-after:avoid;orphans:2;widows:2;text-align:left}h6{padding-top:12pt;color:#666666;font-size:11pt;padding-bottom:4pt;font-family:"Arial";line-height:1.15;page-break-after:avoid;font-style:italic;orphans:2;widows:2;text-align:left}</style></head><body class="c22 c105"><div><p class="c1 c9"><span class="c3"></span></p><h2 class="c1 c13" id="h.vg3pj4ojqw0k"><span>&nbsp;</span></h2></div><p class="c1 c13 c9 title" id="h.5m9a9xm3d0xs"><span></span></p><p class="c1 c9"><span class="c3 c5 c83"></span></p><p class="c1 c13 title" id="h.wnsp797acjk"><span class="c5 c80">The Self-taught Programmer</span></p><p class="c1 c13 title" id="h.vom68ybty87q"><span>____________________________</span></p><p class="c1 c13 title" id="h.vom68ybty87q-1"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p><p class="c1 c13 c95 title" id="h.9rknjkfn9bdp"><span>Cory Althoff</span></p><p class="c1 c9"><span class="c3 c5"></span></p><p class="c1 c9"><span class="c3 c5"></span></p><p class="c1 c9"><span class="c3 c5"></span></p><p class="c1 c9"><span class="c3 c5"></span></p><p class="c1 c9"><span class="c3 c5"></span></p><p class="c1 c9"><span class="c3 c5"></span></p><p class="c1 c9"><span class="c3 c5"></span></p><p class="c1 c9"><span class="c3 c5"></span></p><p class="c1 c9"><span class="c3 c5"></span></p><p class="c1 c9"><span class="c3 c5"></span></p><p class="c1 c9"><span class="c3 c5"></span></p><p class="c1 c9"><span class="c3 c5"></span></p><p class="c1 c9"><span class="c3 c5"></span></p><p class="c1 c9"><span class="c3 c5"></span></p><p class="c1 c9"><span class="c3 c5"></span></p><p class="c1 c9"><span class="c3 c5"></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">Copyright &copy; 2016 by Cory Althoff</span></p><p class="c1"><span class="c3">All rights reserved. This book or any portion thereof</span></p><p class="c1"><span class="c3">may not be reproduced or used in any manner whatsoever</span></p><p class="c1"><span class="c3">without the express written permission of the publisher</span></p><p class="c1"><span class="c3">except for the use of brief quotations in a book review.</span></p><p class="c1"><span class="c3">ISBN 0-9000000-0-0</span><sup><a href="#cmnt1" id="cmnt_ref1">[a]</a></sup></p><p class="c1"><span class="c17 c3"><a class="c18" href="https://www.google.com/url?q=http://www.theselftaughtprogrammer.io&amp;sa=D&amp;ust=1467337425647000&amp;usg=AFQjCNEmjV9ij4MG95lXrCGA80oGCo7UDg">www.theselftaughtprogrammer.io</a></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">This book is dedicated to my parents Abby and James Althoff for always supporting me. </span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c51"><span class="c17"><a class="c18" href="#h.mwbdmiqrsmue">Part I Learn to Program</a></span></p><p class="c1 c10"><span class="c17"><a class="c18" href="#h.ou2br6s9pb3p">Chapter 1. Introduction</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.o3nmgkuykk35">How This Book Is Structured</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.a88gh8erb26z">End Game First</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.kwiw0jc3kos7">The Self-taught Advantage</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.1f670ol33a4e">Why You Should Program</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.pja2yk4cqkum">Sticking With It</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.15m61tjz25ox">Technologies Used In This Book</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.xbhr7ds7wnh8">Skipping Chapters</a></span></p><p class="c1 c10"><span class="c17"><a class="c18" href="#h.fnrkiassfaco">Chapter 2. Getting Started</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.zerqo2wykeok">What is Programming</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.9ilgl1pwv0lt">What is Python</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.64rls3w07rac">Installing Python</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.uumqhdjwu733">Troubleshooting</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.i5a2jfwftfhy">IDLE and the Interactive Shell</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.fy9uzoranqbl">Running Example Programs</a></span></p><p class="c1 c10"><span class="c17"><a class="c18" href="#h.8zsym7fgntfl">Chapter 3. Introduction to Programming</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.op6p1d9iihxk">Conventions used in this Book</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.5zbgjmxm1lr">Comments</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.ukybmuz76w4j">Python Spacing</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.uvy1nhxd8msw">Python as a Calculator</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.h0e8cecjji2f">Types</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.rqkxhrrjw8og">Errors</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.oato4pnccvu">Variables</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.3zmxfgapcgqu">Comparison Operators</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.do265k9aix3t">Assignment vs. Equality</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.cptodeqn1w4w">And Or</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.ukl83ir4f1nj">Control Flow</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.yej81baz0oo2">None</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.rkottose9n2">input</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.u2xh8xe84rt9">Exception Handling</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.lbokdgqkw7iu">Wrapping Up</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.50y00mqvakgy">Challenge</a></span></p><p class="c1 c10"><span class="c17"><a class="c18" href="#h.69xm4mlmvzd">Chapter 4. Functions</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.pa7lyv1dow1t">Defining Functions</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.xryixwf2klxq">Optional Parameters</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.j2jsngtzpj5i">Pass</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.5jv8nsehe0wg">Scope</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.70d9b42lmrfp">Built-in Functions</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.ubswbiqco04h">Built-in Modules</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.fgjb9wmpraax">Functions vs. Methods</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.glb19t1qxls9">Wrapping Up</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.n5kaxcblq7ei">Challenge</a></span></p><p class="c1 c10"><span class="c17"><a class="c18" href="#h.rzxed3nea8gn">Chapter 5. String Manipulation</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.7k9ccz8m2c1g">Change Case</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.d5kpnw9gyhfp">In</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.stfy07jf9t7i">Escaping Strings</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.pws0uu4mqol6">Index</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.lwi2k2j81mpk">Format</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.qkv2d8do36kn">\n</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.8skl39qwbnwc">Split</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.kg5by1p6m7by">Join</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.c6yi104u3ruk">Wrapping Up</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.gvz6yazddsxt">Challenge</a></span></p><p class="c1 c10"><span class="c17"><a class="c18" href="#h.bxron7y2q3ek">Chapter 6. Containers</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.m7y63rpmtmbw">Lists</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.mdvpxuvfb30e">Tuples</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.6ahhemnexhhk">Dictionaries</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.e1vw8hbaukhm">Wrapping Up</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.dughv998w309">Challenge</a></span></p><p class="c1 c10"><span class="c17"><a class="c18" href="#h.si7tzj1fsrml">Chapter 7. Loops</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.6fat0mgaqsm">For Loops</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.h6489vu7663k">While Loops</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.k8jzaxkif0dq">Nested Loops</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.o05h5unmo6qs">Break</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.57m96sr3sr8e">Continue</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.z8axj6c07xr">Wrapping Up</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.9v0vvm1gaug0">Challenge</a></span></p><p class="c1 c10"><span class="c17"><a class="c18" href="#h.e20r9o8e78ck">Chapter 8. Modules</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.gu80v3nwzts">Creating a Module</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.9ahcnyksia9u">Dot Notation</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.fokw2739tw64">Troubleshooting Imports</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.lp6hdlau3olo">Wrapping Up</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.s3bllsvku6cn">Challenge</a></span></p><p class="c1 c10"><span class="c17"><a class="c18" href="#h.8x19bmyu93qt">Chapter 9. Files</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.5nqsmwtzcwkc">Working With Files</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.8kgras3xizrh">Using with</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.aa8bphpnsja2">Reading Files</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.nv20yhyqhjmf">CSV Files</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.9s2c2stcczxf">Wrapping Up</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.sezzad5njwxc">Challenge</a></span></p><p class="c1 c10"><span class="c17"><a class="c18" href="#h.cgcuqoku04uo">Chapter 10. Let&rsquo;s Read Some Code</a></span></p><p class="c1 c10"><span class="c17"><a class="c18" href="#h.8p4qs5ghjdcy">Chapter 11. Bringing It All Together</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.z70y9ptatqku">Hangman</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.wqrs4kmfcq82">Wrapping Up</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.rj15pg5f4vod">Challenge</a></span></p><p class="c1 c10"><span class="c17"><a class="c18" href="#h.ctkubigcafm3">Chapter 12. Practice</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.11bmgoljb6tg">Exercises</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.9kluowq1vpg2">Read</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.apk3pslpmgco">Getting Help</a></span></p><p class="c1 c51"><span class="c17"><a class="c18" href="#h.nznwuon0i2sk">Part II Learn to Program with Objects</a></span></p><p class="c1 c10"><span class="c17"><a class="c18" href="#h.u3vnqd9y9yck">Object-oriented Programming</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.n2243pisgegu">Everything is an Object</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.nbpycovbjrmt">Wrapping Up</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.2yz4z742ssif">Challenge</a></span></p><p class="c1 c10"><span class="c17"><a class="c18" href="#h.fcj5558yrccw">More Object-oriented Programming</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.7k3bjvzeybrm">Assignment</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.u9osed5xbgtc">is</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.l8wdd4brgtyy">Class Variables</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.lwnc0kieeblu">Inheritance</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.dn3ow6fv2grf">Overriding</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.n46r1qqjydcu">Private variables</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.tdkf6rgc3uvt">Wrapping Up</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.x93274umj49q">Challenge</a></span></p><p class="c1 c10"><span class="c17"><a class="c18" href="#h.ggeenz74x8l2">Let&rsquo;s Read Some Code</a></span></p><p class="c1 c10"><span class="c17"><a class="c18" href="#h.qmvjb9nenvlt">Bringing It All Together</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.5sd9xegbfl34">War</a></span></p><p class="c1 c10"><span class="c17"><a class="c18" href="#h.n6yu3d4nzybt">Practice</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.g67uwk2urwzh">Exercises</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.jnh4tpoqy5ix">Read</a></span></p><p class="c1 c51"><span class="c17"><a class="c18" href="#h.mkbi036p4c76">Part III Learn to use Tools to Enhance your Performance</a></span></p><p class="c1 c10"><span class="c17"><a class="c18" href="#h.4z7egkfdttuy">Virtual Machines</a></span></p><p class="c1 c10"><span class="c17"><a class="c18" href="#h.9djb3u12svy">Reading Documentation</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.33vla5i5gpfz">Brackets [ ]</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.af081k72e7s5">Ellipses ...</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.oxpygbb8fv0y">pipes |</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.jraohdkniwux">Flags -F</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.o4sc50cju1t">Dollar Sign $</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.4lsii529u2nk">Programming Conventions</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.ks5z1jxxq7xd">Wrapping Up</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.v5gdcefcys2c">Documentation Challenge</a></span></p><p class="c1 c10"><span class="c17"><a class="c18" href="#h.6t6s5xazb26u">The Command Line</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.60xxv3thb2e">Getting Setup</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.h19tr3dl2wcm">echo</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.mr9krpmrbof6">Bash</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.gvjmjpn547m">Navigating</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.1p2wmwhuj4ht">Relative vs Absolute</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.hl56hkbp64it">Wildcard</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.kq114m6usgxn">Hidden Files</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.q2j31jn2qebw">View A File With Less</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.2wnhuze4oy72">Environmental Variables</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.h6lfnmhvatol">$PATH</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.r2psbwpiyc4s">which</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.hlxe8ozewnge">Users</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.jg9947bx97nz">sudo</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.66p418cud0eq">Permissions</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.7emxiu9mqc2k">Change Permissions</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.hwmgff64angj">Executables</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.cui36gbc9lsu">pipes</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.zi5dmafyk54k">Jump Around</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.ze01lavdqpac">Tab Complete</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.sz1zima4qa4z">cat</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.qlfxdseo880l">SSH</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.xogdw2ont4q7">cron</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.p68p7r198w0o">history</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.6nssgqwwgess">Build a Command Line Program</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.mgistxt5xhod">Other Tools</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.ce6yh38q0rp">Wrapping Up</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.g1zy03v5xan">The One Week Challenge</a></span></p><p class="c1 c10"><span class="c17"><a class="c18" href="#h.qdmtlgv3pxw">VIM</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.iarkki13b6ht">Setup</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.86a2yktzg4mf">Normal Mode</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.rsre6tlb0jrn">Insert Mode</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.e7zi9dalv15c">Jumping Around</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.d76n05v2mdc1">Deleting</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.llieczr4ay8s">Exiting Vim</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.26aksbb9uush">Wrapping Up</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.ur8l56w7mgle">VimTutor Challenge</a></span></p><p class="c1 c10"><span class="c17"><a class="c18" href="#h.i7w0bdaedvos">Regular Expressions</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.vvpk46adrjfr">Setup</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.bt2t65vzi03f">Simple Match</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.sdw8swac5nvj">Ignore Case</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.p1up3kjutvp6">Only Return Matched</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.t64otbmjyh2l">Match Beginning and End</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.regugyg5up2m">Brackets</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.ndlgzvn4w8s3">Repetition</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.i6xixw09m45">Range</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.zd5v9g61g86b">Match Between</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.xqtgwjh5ehfo">Escaping</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.c579tvwymfvb">grep -E</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.v6pl4ylpwko4">More Repetition</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.lcaf6b8t881t">Wrapping Up</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.1vf4awosh5jy">Zen Challenge</a></span></p><p class="c1 c10"><span class="c17"><a class="c18" href="#h.4vrq1xqluo2t">Package Managers</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.eh79ntd33rdw">Homebrew</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.q6sudd7keew">Apt-get</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.kd0r6hj5hd44">pip</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.ocl7p9k386su">Wrapping Up</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.hptg87me05cy">New Software Challenge</a></span></p><p class="c1 c10"><span class="c17"><a class="c18" href="#h.9nrq8rr7ac2z">Version Control</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.41asqaaoe1ax">Getting Started</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.9z1ggmn0ic0">Repositories</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.hm3zvtkhbwxp">Pushing</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.74qai0k92i57">Pulling</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.v1yveyvzylas">Syncing our repository</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.aqc0y49pmus9">Pulling Example</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.rvynwx6ci41t">Pushing Example</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.84tcmxa0x1np">Reverting Versions</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.6sx1o0qlabk5">diff</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.erd2oymrigp">Branching</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.ftkq3fo8s2n3">Merging</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.d0uu1277h4ui">The Other Pull Request</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.k8ihvo45m8ho">Clone a Project</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.vytqmc4n1b0p">Wrapping Up</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.vhytjap0j9ny">Challenge</a></span></p><p class="c1 c10"><span class="c17"><a class="c18" href="#h.ldebqocqaxgk">Using an IDE</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.i2cna09s2ut">Jump to a Definition</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.izg9vkebcp1u">Jump to a Line</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.ogdyohlqke3d">Save Local History</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.s5pn9dd0s1tq">Move Code Up &amp; Down</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.8ldicw9l3o4z">Version Control</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.wzazslu75quq">Command Line</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.sw1is7o193jb">Database Access</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.o01ia077le9w">Debugging</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.hhmrygw2rfca">Wrapping Up</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.uq77h4glim8v">Challenge</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.dskav866zyk0">Practice</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.dskav866zyk0">Exercises</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.428djalh244">Read</a></span></p><p class="c1 c10"><span class="c17"><a class="c18" href="#h.52urb71vxnoy">Let&rsquo;s Read Some Code</a></span></p><p class="c1 c10"><span class="c17"><a class="c18" href="#h.2v9hx1dfl3u0">Bringing It All Together</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.ritur7xu47m3">HTML</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.sn1ssbd6koei">Scrape Google News</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.xpra7190ndeo">Wrapping Up</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.dogv21if61ja">Challenge</a></span></p><p class="c1 c51"><span class="c17"><a class="c18" href="#h.iboyy11j25wv">Part IV Study Computer Science</a></span></p><p class="c1 c10"><span class="c17"><a class="c18" href="#h.yhwshf5cz9zs">Computer Architecture</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.yxytc36erslc">CPU</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.hhkw9dxis0fa">Memory</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.hth65tvubinq">Devices</a></span></p><p class="c1 c23"><span class="c17"><a class="c18" href="#h.b73yaagxx6vh">I/O</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.jogl9q6rf2ja">Network</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.f0w897framwh">Wrapping Up</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.4lss9raky8vb">Challenge</a></span></p><p class="c1 c10"><span class="c17"><a class="c18" href="#h.rx2tv5c4th4w">Low Level</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.kj35lqq1pd9s">Binary</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.fcmct7vaivuo">High-level vs. Low Level Languages</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.fde5b89kb9d7">Dynamic vs. Statically Typed Languages</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.5guxwmfw6uw">Math</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.clgqz0r5o9h4">Character Representation</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.48cp62o4ibvv">Wrapping Up</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.r1b9ddl7csd4">Challenge</a></span></p><p class="c1 c10"><span class="c17"><a class="c18" href="#h.otwq7pynty22">Data Structures &amp; Algorithms</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.ospe9hf18tpz">What Are Algorithms &amp; Data Structures?</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.y9pg0zkthft1">Big O Notation</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.fq1ereenkrkw">Modulo</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.rapqos3xtqu5">Bubble Sort</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.fyqh56v8nysv">Sequential Search</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.ju8sjaom4d1x">Binary Search</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.qzy5m4gvj0pj">Recursion</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.bd5n41gpj60j">Abstract Data Types</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.23539d9hay73">Nodes</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.9cr268jlayfc">Stacks</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.bpsl2a6lud5c">Linked Lists</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.rakpjxvfvhqa">Arrays</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.9t8mghwa8suf">Binary Trees</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.luujnwxy30gf">Breadth First &amp; Depth First Search</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.3m5ekm4v8y4p">Hash Tables</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.4pa7hqbox18k">Wrapping Up</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.8iwvho801ojd">Challenge</a></span></p><p class="c1 c10"><span class="c17"><a class="c18" href="#h.evyjmvvmusct">Operating Systems</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.r1i62bi3e752">The Kernel</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.a0e364d8u33u">Scheduling</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.x0lrsf8ew3ep">Threading</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.x0lrsf8ew3ep">Concurrency vs. Parallelism</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.eekfahpr5ick">Wrapping Up</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.twrvf7kpkzxp">Challenge</a></span></p><p class="c1 c10"><span class="c17"><a class="c18" href="#h.jiqehctv5luc">Compilers</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.6edn32xnz0lo">Interpreted Vs. Compiled</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.lr1ew1hp5296">Build a Compiler</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.vscat67n1g05">Wrapping Up</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.leff01dyxqf6">Challenge</a></span></p><p class="c1 c10"><span class="c17"><a class="c18" href="#h.j7bpd5zfuhc6">Object-oriented Programming</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.vni5wabsaggh">Programming Paradigms</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.f3c76ydkpvdv">The Four Pillars of OOP</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.v11cv3bdvhqp">Composition</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.xe51b2b682o2">Polymorphism</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.7y1hedh3xa82">Abstraction</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.b3dfebxn6hmt">Encapsulation</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.1tgg331mmwet">MVC</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.y39isd8drzrf">Design Patterns</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.svo5oqkxwj9x">Wrapping Up</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.qxym5wqsxm9l">Challenge</a></span></p><p class="c1 c10"><span class="c17"><a class="c18" href="#h.9qrrg0plrgmq">Databases</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.dy5obijdujt7">NoSQL vs. SQL</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.4wg9o8f1z36y">Getting Started</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.39rghkqp7dqn">Coming Back to Your DB</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.pg4qorxkrq3a">Data Types</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.yu7be6x51ohm">Create a Table</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.am7a626tn4gf">Constraints</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.afu2wxnagmfi">Insert Data</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.fkjz2vj37mu">Query Data</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.qudgt5eidyev">Or Query</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.tw5l12h7d7ks">And Query</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.632snz1pmey1">Joins</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.jmglyz18ck1b">Count</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.oa9kar919z2x">Relational Database Design</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.etdu767q0pyq">Normalization</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.pr89pksvjlnd">Referential Integrity</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.97nl23hat3qb">Indexing</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.pko7t2l129xh">Communicating with Databases</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.l4oak9u4wyd">Wrapping Up</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.f89ut8zg1mif">Challenge</a></span></p><p class="c1 c10"><span class="c17"><a class="c18" href="#h.ofz6411l2obb">Network Programming</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.8c4tue18974n">Client Server Model</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.uwpk3k5m66ai">TCP/IP</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.kgle1c7wjkza">Create a Server</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.6m2tpn2xgw7i">Create a Client</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.a46i61dj3b31">Wrapping Up</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.bx8j1t8a8rxv">Challenge</a></span></p><p class="c1 c10"><span class="c17"><a class="c18" href="#h.b8x0sigv63ht">Security</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.l1z6wy9z8nvz">Don&rsquo;t Log in As Root</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.qitudi67xmk3">Never Trust User Input</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.qzh2okbi74q0">Minimize Your Attack Surface</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.g1l63mpxx1b2">Wrapping Up</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.vvm89e4elre8">Challenge</a></span></p><p class="c1 c10"><span class="c17"><a class="c18" href="#h.jgqe3p9izeig">Let&rsquo;s Read Some Code</a></span></p><p class="c1 c10"><span class="c17"><a class="c18" href="#h.40cezcimkyp3">Bringing It All Together</a></span></p><p class="c1 c10"><span class="c17"><a class="c18" href="#h.23dgjxqviczf">Practice</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.23dgjxqviczf">Exercises</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.23dgjxqviczf">Read</a></span></p><p class="c1 c51"><span class="c17"><a class="c18" href="#h.e77hbc4l6ore">Part V Learn Best Practices</a></span></p><p class="c1 c10"><span class="c17"><a class="c18" href="#h.6goz48hyiage">Testing</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.sapah5m97x7t">Testing Saves Time</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.ut5i8p70m27z">Unit Tests</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.4ka7jtghhssq">Assertions</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.tum102n5bj45">Unit Testing Framework</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.gej7w7g7k8b0">Example</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.oh2vbncgjoac">Setup &amp; Teardown</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.3c9jw0ho09p">Regression Tests</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.bx5tik9bufzj">TDD</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.vc47d8fuk4o5">Wrapping Up</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.5q5gzslsjg53">Challenge</a></span></p><p class="c1 c10"><span class="c17"><a class="c18" href="#h.jazegstwlbin">Logging</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.a6gjeyv809c3">Logging Exceptions</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.l16dglvkw35u">Collecting Data</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.i9bz4psqrgm3">Logging Levels</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.umsj41xtbqij">Wrapping Up</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.4yw7hkvt3hll">Challenge</a></span></p><p class="c1 c10"><span class="c17"><a class="c18" href="#h.9c07fn2a38ph">Good Programming Practices</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.2xng54kbfwid">Write Code As A Last Resort</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.6mpvlnkhtofi">DRY</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.jxenhfn942w8">Orthogonality</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.mn2qvjudah8h">Every Piece Of Data Should Have One Representation</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.aoeo0rhwhbqk">Functions Should Do One Thing</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.nw4vwp4kejkv">Docstrings</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.7v5smg52hth1">Use Dummy Data</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.43vf8ayqp2b8">If It&rsquo;s Taking Too Long You Are Probably Making a Mistake</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.eqyjrlfze1sv">Do Things The Best Way The First Time</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.bxhvb9txkik2">Follow Conventions</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.6oxbttzfaki0">Code Reviews</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.eney32hjes5">Pep-8</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.426d3t89vvm">Separate Logic</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.rlv65dlkr00c">Challenge</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.xjmiqmvvxffu">Code Improvement Challenge</a></span></p><p class="c1 c10"><span class="c17"><a class="c18" href="#h.vdiplircjll5">Let&rsquo;s Read Some Code</a></span></p><p class="c1 c10"><span class="c17"><a class="c18" href="#h.alfxjz2tc8tp">Bringing It All Together</a></span></p><p class="c1 c10"><span class="c17"><a class="c18" href="#h.hil10kqqkydp">Practice</a></span></p><p class="c1 c51"><span class="c17"><a class="c18" href="#h.go01qf1u1nwk">Part VI Land a Job</a></span></p><p class="c1 c10"><span class="c17"><a class="c18" href="#h.fic869m5mrbk">Your First Programming Job</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.yj6z9xu8sihc">Getting Initial Experience</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.fk6kkjkx8rcj">Choose a Path</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.qb9qvyl62p21">Getting an Interview</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.noytbq5dh2oc">The Interview</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.sgjw9ft6t6ff">Hacking The Interview</a></span></p><p class="c1 c10"><span class="c17"><a class="c18" href="#h.ur98wqjimxe3">Working on a Team</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.scwvb06zh0cg">Master the Basics</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.2q5gsq885ss1">Don&rsquo;t Ask What You Can Google</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.rtv34luwjp6m">Changing Code</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.4tic2b1lo48c">Imposter Syndrome</a></span></p><p class="c1 c10"><span class="c17"><a class="c18" href="#h.8x7jkdckc5d9">Further Learning</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.zcvgns7quxw8">The Classics</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.gafsiilgv7q3">Replacing Classics</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.5pn903in0jyx">CodeSchool</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.3khufknsvlxz">Hacker News</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.54jkghjmx169">Other</a></span></p><p class="c1 c10"><span class="c17"><a class="c18" href="#h.bty722mwqr97">Next Steps</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.inzb4q6f2txm">Find a Mentor</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.2w3b4apad3y9">Strive to Go Deep</a></span></p><p class="c1 c14"><span class="c17"><a class="c18" href="#h.w24wxpj7iuxu">Other Advice</a></span></p><p class="c1 c10"><span class="c17"><a class="c18" href="#h.7fobi1qbvu6e">Acknowledgements</a></span></p><p class="c1 c10"><span class="c17"><a class="c18" href="#h.n3i5lsxfrou2">Citations</a></span></p><p class="c1 c9"><span class="c3"></span></p><h1 class="c1 c13" id="h.mwbdmiqrsmue"><span>Part I Learn to Program</span></h1><h2 class="c1 c13" id="h.ou2br6s9pb3p"><span>Chapter 1. Introduction</span></h2><p class="c1"><span class="c3">&quot;Most good programmers do programming not because they expect to get paid or get adulation by the public, but because it is fun to program.&quot;</span></p><p class="c1"><span class="c3">&mdash; Linus Torvalds</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">I majored in Political Science at Clemson University. When I was deciding what to major in, I considered Computer Science. I even enrolled in an Introduction to Programming class my Freshman year, but quickly dropped it once the class started. It was too inaccessible. After I graduated, I was living in Silicon Valley and decided I needed to learn to program. A year later, I was working as a software engineer II at eBay. I don&rsquo;t want to give the impression that this was easy. It definitely wasn&rsquo;t. It was incredibly challenging. In between throwing things at the wall, it was a lot of fun too. &nbsp;</span></p><p class="c0"><span class="c3">I started my journey by learning to program in Python. This book, however, is not about teaching you how to program in a specific language. There are plenty of amazing books, classes and resources that do that already. While this book does teach you some Python, it&rsquo;s about everything else these resources do not teach you. It&rsquo;s about the things I had to learn on my own in order to become a software engineer. This book is not meant for someone looking for a casual introduction to programming, so they can write code as a hobby. This book is written specifically for people looking to become professional programmers.</span></p><p class="c0"><span class="c3">Learning a programming language is only part of the battle. There are other skills you need to learn in order to speak the language of computer scientists and become a successful software engineer. &nbsp;This book will teach you everything I learned on my journey from programming novice to professional software engineer. I wrote this book to give aspiring programmers an outline of what they need to know. As a self-taught programmer, I didn&rsquo;t know what I didn&rsquo;t know. I didn&rsquo;t know what I needed to learn. The introduction to programming books are all the same. They teach you the basics of how to program in either Python or Ruby and send you on your way. The feedback I&rsquo;ve heard from people finishing these book is, &ldquo;What do I do now? I am not a programmer yet, and I don&rsquo;t even know what to work on next.&rdquo; This book is my answer to that question.</span></p><h3 class="c1 c13" id="h.o3nmgkuykk35"><span>How This Book Is Structured</span></h3><p class="c0"><span class="c3">This book is divided into five parts, based on moving through the following stages: learning to program, learning to program well, learning to use tools to program more efficiently, learning Computer Science, and learning best practices. </span></p><p class="c0"><span class="c3">Many of the subjects covered in one chapter in this book could be, and are, covered by entire books. My goal is not to cover every detail. My goal is to give you a map, an outline of all of the skills you need to develop in order to become a professional programmer. The first section is an introduction to programming. The goal of this section is to get you to write your first program as quickly as possible, hopefully today. If you already know how to program, you can use the introduction to programming section to start learning Python. If you already know Python, you should probably skip it. </span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;The second section of this book is about taking your programming skills to the next level. We learn object-oriented programming and build an awesome, yet simple program that will show you the power of programming, and get you hooked. </span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;In the third section, we learn to use different tools to take your programming productivity to the next level. At this point, you are hooked on programming, and the only thing on your mind is getting even better. You will learn how to use the command line and how to collaborate with other engineers using version control. You will learn how to use your Interactive Development Environment to boost your productivity, and how to install and manage third party programs.</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;The fourth section is all about Computer Science. Now that you can program, you will have all kinds of questions about how everything works. This section is where a lot of those questions get answered. </span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;The final section is about learning how to further improve your skills, and preparing you for your first job as a programmer. We go over how to prepare for a technical interview, as well as how to successfully work with your new team. </span></p><p class="c0"><span class="c3">If you don&rsquo;t have any programming experience, you will need to utilize other resources in addition to this book (I provide resources to explore at the end of each section) and you shouldn&rsquo;t try to read this book too quickly. Instead, you can use this book as a guide, practicing for as long as you need in between sections, using the recommended resources. However, no matter what your skill level, there is no need to wait before diving into the first section, and, if you already have some experience, you should have no problem reading this book straight through. </span></p><h3 class="c1 c13" id="h.a88gh8erb26z"><span>End Game First</span></h3><p class="c0"><span class="c3">The way I learned to program is the opposite of how Computer Science is traditionally taught, and I structured the book to follow this approach. Traditionally, you spend a lot of time learning theory. So much so, that many Computer Science graduates come out of school not knowing how to program. In his blog, </span><span class="c15 c3">Why Can&rsquo;t Programmers..Program?, </span><span class="c3">Jeff Atwood writes: &ldquo;Like me, the author is having trouble with the fact that </span><span class="c3">199 out of 200</span><span class="c3">&nbsp;applicants for every programming job can</span><span class="c3">&rsquo;</span><span class="c3">t write code at all. I repeat: they can</span><span class="c3">&rsquo;</span><span class="c3">t write any code whatsoever.&rdquo; This led to the development of the FizzBuzz coding challenge, a programming test used in interviews to weed out candidates, and why we spend so much of this book learning skills you will use in practice. Don&rsquo;t worry, we also learn how to pass FizzBuzz.</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;In </span><span class="c15 c3">The Art of Learning</span><span class="c3">, Josh Waitzkin of </span><span class="c15 c3">Searching for Bobby Fischer</span><span class="c3">&nbsp;fame, describes how he learned how to play chess in the reverse order most people do. Instead of studying opening moves, he started learning the end game (where there are only a few pieces left) first. This gave him a better understanding of the game, and &nbsp;allowed him to go on to win many championships. Similarly, I think it is more effective to learn to program first, and learn theory later once you are dying to know how everything works under the hood. That is why I wait until the fourth section of the book to cover the most import parts of Computer Science theory. Theory is important, and it will be even more valuable to you once you already have experience programming. </span></p><h3 class="c1 c13" id="h.kwiw0jc3kos7"><span>The Self-taught Advantage</span></h3><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Learning how to program outside of school is increasingly common. A 2015 Stack Overflow survey found forty eight percent of respondents did not have degrees in computer science </span><span class="c41 c3">10</span><span class="c3">. Which brings me to another goal of this book&mdash;for you to learn enough to avoid feeling intimidated by your teammate with a CS degree from Stanford once you get your first job as a programmer. When I was hired at eBay, I was on a team with CS degrees from Stanford, Cal, and Duke, as well as two Physics </span><span class="c3">PhD&rsquo;s</span><span class="c3">. At 25, it was intimidating that my 21 year old teammates knew ten times more about programming than I did. My hope is that with this book, I will help you close that gap by making you a more knowledgeable software engineer. </span></p><p class="c0"><span class="c3">As intimidating as it may be to work with people with bachelor</span><span class="c3">&rsquo;</span><span class="c3">s, master</span><span class="c3">&rsquo;</span><span class="c3">s and P</span><span class="c3">h</span><span class="c3">D&rsquo;s in Computer Science, never forget you have what I like to call the self-taught advantage. You are not reading this book because a teacher assigned it to you, you are reading it because you have a desire to learn, and wanting to learn is the biggest advantage you can have. </span></p><h3 class="c1 c13" id="h.1f670ol33a4e"><span>Why You Should Program</span></h3><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;If you are reading this book, I assume you&rsquo;ve already decided you want to learn to program. However, I&rsquo;m still going to go over why you should program to provide some extra motivation. Learning to program is empowering and rewarding. There is an insane demand for good engineers right now. By 2020, an estimated one million programming jobs will go unfilled </span><span class="c3 c41">45</span><span class="c3">. I love coming up with new ideas, and I always have a new project I want to work on. Programming gives me the ability to sit down and build the ideas I have in my head without needing permission from anyone. In the past, I had to hire someone to do it for me. This was expensive and never turned out well. Being able to think of a good idea, and start building ten minutes later is empowering. &nbsp;On top of that, programming will make you be better at everything. Seriously. There aren&rsquo;t many subjects in the world that don&rsquo;t benefit from finely tuned problem solving skills. You will be able to apply your skills to all kinds of different areas. I recently was tediously looking for housing on Craigslist, and was able to write a script to look for me and email me the results. Learning to program will free you from repetitive tasks forever.</span></p><h3 class="c1 c13" id="h.pja2yk4cqkum"><span>Sticking With It</span></h3><p class="c1"><span class="c3 c5">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">If you don&rsquo;t have any programming experience, and are nervous about making this journey, I want you to know you are absolutely capable of doing it. It is a common misconception that programmers need to be really good at math; they don&rsquo;t. You are going to need to put in hard work, but a lot of this stuff is easier to learn than you think. The only thing that can hold you back is not sticking with it, so let&rsquo;s go over some ways to make sure you do. &nbsp;I used a checklist to make sure I practiced every day. If you can get into the habit of doing this, you should&mdash;it really helped me stay focused. I use Wunderlist for my daily checklist. </span></p><p class="c0"><span class="c3">You can also use a friend or family member to motivate you. Not in the usual way&mdash;give them money with the instructions that it is either to be returned to you upon finishing this book within a timeframe of your choosing, or donated to an organization you dislike, like the American Nazi Party (this is a technique Tim Ferriss uses to help motivate himself). &nbsp;</span></p><h3 class="c1 c13" id="h.15m61tjz25ox"><span>Technologies Used In This Book</span></h3><p class="c1"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">In order to help readers of this book learn as much as possible, this book teaches certain technologies. For example, in the chapter Version Control, we go over the basics of using Git. I chose Git because I consider it the industry standard for version control. </span></p><p class="c0"><span class="c3">There are three types of operating systems you should be familiar with: Windows, Unix and Linux. Windows is Microsoft&rsquo;s operating system that comes with PCs. Unix is an operating system created in the 1970&rsquo;s. An operating system is allowed to use the Unix name if it adheres to the Single UNIX Specification. Currently, the most popular Unix operating system is Apple&rsquo;s OS X. Linux is an open source operating system used by the majority of servers in the world. Open source means anyone can help improve it, and it&rsquo;s not owned by a company. Linux is considered a Unix-like operating system. This book assumes you are using either a Mac, PC, or Ubuntu . If you are using a PC, you will need to either have access to a Linux machine, or setup a virtual machine, in order to work your way through the third section of this book. Don&rsquo;t worry, we go over setting up a virtual machine in part three. &nbsp;</span></p><p class="c0"><span class="c3">I use Python for the majority of programming examples. I chose Python because it is a popular first programming language to learn, and it&rsquo;s a very easy language to read, even if you&rsquo;ve never used it. It also doesn&rsquo;t hurt that there is a huge demand for Python developers at companies in just about every field. </span></p><p class="c0 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.z80e067ftvzi"><span>Change</span><sup><a href="#cmnt2" id="cmnt_ref2">[b]</a></sup></h3><p class="c1"><span>#talk about how technology changes and steps to keep this book as timeless as possible taken in this book</span></p><h3 class="c1 c13" id="h.xbhr7ds7wnh8"><span>Skipping Chapters</span></h3><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;The chapters in this book build on one another. If you see something you don&rsquo;t understand, it probably was covered in another chapter. I try not to re-explain concepts covered in previous chapters, so for example after the chapter VIM in Part III, I expect you to be able to create a file using Vim.</span></p><h2 class="c1 c13" id="h.fnrkiassfaco"><span>Chapter 2. </span><span>Getting Started</span></h2><p class="c1"><span>&ldquo;A good programmer is someone who always looks both ways before crossing a one-way street.&rdquo; </span></p><p class="c1"><span>&mdash;Doug Linder</span></p><p class="c1 c9"><span class="c101 c81"></span></p><h3 class="c1 c13" id="h.zerqo2wykeok"><span>What is Programming</span></h3><p class="c1"><span class="c35 c3 c5">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">Programming means writing instructions for a computer to carry out. The instructions may tell the computer to do anything from printing &ldquo;Hello,&rdquo; to scraping data from the internet, to reading the contents of a file and saving them to a database. Programmers write these instructions in different programming languages. In the past, programming was much harder, as programmers were forced to use cryptic, low-level programming languages like Assembly. Here is an example of what a simple program looked like:</span></p><p class="c1 c9"><span class="c11 c3"></span></p><p class="c1 c10 c53"><span class="c47 c4">global &nbsp;_start</span></p><p class="c1 c53 c10"><span class="c47 c4">&nbsp; &nbsp; &nbsp; &nbsp; section .text</span></p><p class="c1 c53 c10"><span class="c47 c4">_start:</span></p><p class="c1 c53 c10"><span class="c47 c4">&nbsp; &nbsp; &nbsp; &nbsp; mov &nbsp; &nbsp; </span><span class="c59 c4">rax</span><span class="c47 c4">, 1 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span></p><p class="c1 c53 c10"><span class="c47 c4">&nbsp; &nbsp; &nbsp; &nbsp; mov &nbsp; &nbsp; </span><span class="c59 c4">rdi</span><span class="c47 c4">, 1 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span></p><p class="c1 c53 c10"><span class="c47 c4">&nbsp; &nbsp; &nbsp; &nbsp; mov &nbsp; &nbsp; </span><span class="c59 c4">rsi</span><span class="c47 c4">, message &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span></p><p class="c1 c53 c10"><span class="c47 c4">&nbsp; &nbsp; &nbsp; &nbsp; mov &nbsp; &nbsp; </span><span class="c59 c4">rdx</span><span class="c47 c4">, 13 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span></p><p class="c1 c53 c10"><span class="c47 c4">&nbsp; &nbsp; &nbsp; &nbsp; syscall &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span></p><p class="c1 c53 c10"><span class="c47 c4">&nbsp; &nbsp; &nbsp; &nbsp; </span><span class="c15 c4 c27">; exit(0)</span></p><p class="c1 c53 c10"><span class="c47 c4">&nbsp; &nbsp; &nbsp; &nbsp; mov &nbsp; &nbsp; </span><span class="c4 c59">eax</span><span class="c47 c4">, 60 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span></p><p class="c1 c53 c10"><span class="c47 c4">&nbsp; &nbsp; &nbsp; &nbsp; xor &nbsp; &nbsp; </span><span class="c59 c4">rdi</span><span class="c47 c4">, </span><span class="c59 c4">rdi</span><span class="c47 c4">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span></p><p class="c1 c53 c10"><span class="c47 c4">&nbsp; &nbsp; &nbsp; &nbsp; syscall &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span></p><p class="c1 c53 c10"><span class="c4 c47">message:</span></p><p class="c1 c53 c10"><span class="c47 c4">&nbsp; &nbsp; &nbsp; &nbsp; db &nbsp; &nbsp; &nbsp;&quot;Hello&quot;, 10 &nbsp; &nbsp; &nbsp;</span></p><p class="c1 c53 c10"><span class="c72">43</span></p><p class="c1 c9"><span></span></p><p class="c1"><span class="c3">Here is the same program is written in a modern programming language:</span></p><p class="c1 c9"><span></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(&quot;Hello World&rdquo;)</span></p><p class="c1 c9"><span></span></p><p class="c1"><span class="c3">As you can see, programmers today have it much easier&mdash;you don&rsquo;t need to spend time learning cryptic programming languages in order to program. Instead, we will be learning to use an easy to read programming language called Python. </span></p><p class="c1"><span class="c11 c3">&nbsp; &nbsp;</span><span class="c11 c3">&nbsp; &nbsp; &nbsp;</span></p><h3 class="c1 c13 c94" id="h.9ilgl1pwv0lt"><span>What is Python</span></h3><p class="c1"><span class="c81 c101">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">Python is an open source programming language created by Guido van Rossum, and named after Monty Python. It is widely considered one of the best first programming languages to learn because code written in Python is so easy to read. In fact, one of Guido van Rossum&rsquo;s key insights was that programmers spend more time reading code than they do writing it, which is why he created such an easy-to-read language. Python is one of the most popular programming languages in the world. It runs on all major operating systems, and is used in everything from building web servers to creating desktop applications. As you can imagine, this creates a large demand for Python programmers. &nbsp;</span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.64rls3w07rac"><span>Installing Python</span><sup><a href="#cmnt3" id="cmnt_ref3">[c]</a></sup><sup><a href="#cmnt4" id="cmnt_ref4">[d]</a></sup><span>&nbsp; </span></h3><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;In order to follow the examples in this book, you will need Python 3 installed. You can download Python for Windows, OS X, and Ubuntu for free at python.org/downloads. </span></p><p class="c1"><span class="c3">You should download the latest version. </span><span class="c3 c5">Make sure you download Python 3, not Python 2. Some of the examples in this book will not work if you download Python 2.</span><span class="c3">&nbsp; Python is available for both 32 bit and 64 bit computers for every operating system. If you have a new computer purchased after 2007, it is most likely 64 bit. If you aren&rsquo;t sure if your computer is 32 bit or 64 bit, google how to tell if your computer is 32 bit or 64 bit. I apologize if this seems harsh, but so much of programming comes down to googling things you don&rsquo;t understand, that you should start practicing now.</span></p><p class="c0"><span class="c3">If you are on Windows or a Mac, download the 64 or 32 bit version of Python, then click &nbsp;on the file you downloaded and follow the instructions. If you are on Ubuntu, you can download Python with: </span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">Enter sudo apt-get install python3.</span></p><p class="c1"><span class="c3">Enter sudo apt-get install idle3.</span></p><p class="c1"><span class="c3">Enter sudo apt-get install python3-pip.</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">If you aren&rsquo;t on Ubuntu, and those instructions look intimidating to you, don&rsquo;t worry, we will learn what they mean later on. </span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c94 c1 c13" id="h.uumqhdjwu733"><span>Troubleshooting</span></h3><p class="c0"><span class="c3">From this point forward, I will assume you have Python installed. If for any reason you are unable to install Python, please skip ahead to the last chapter of Part I titled &ldquo;Getting Help&rdquo;. &nbsp; </span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.i5a2jfwftfhy"><span>IDLE and the Interactive Shell</span></h3><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Your download comes with two parts; when you finish the download, both should be available on your </span><span class="c3">Programs menu</span><span class="c3">&nbsp;(PC) or Finder (Mac)</span><sup><a href="#cmnt5" id="cmnt_ref5">[e]</a></sup><span class="c3">. </span></p><ol class="c45 lst-kix_x8hveywbunjm-0 start" start="1"><li class="c1 c10 c38"><span class="c3">Interactive shell. If you click on the Python icon, you get the interactive shell, which opens with these lines (although this could change so don&rsquo;t worry if the message is absent or different):</span><span class="c3"><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Python 3.5.1 (v3.5.1:37a07cee5969, Dec &nbsp;5 2015, 21:12:44) <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Type &quot;copyright&quot;, &quot;credits&quot; or &quot;license()&quot; for more information.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt;&gt;</span></li><li class="c1 c10 c38"><span class="c3">IDLE. </span><span class="c3">If you click on the IDLE icon</span><span class="c3">, you get the t</span><span class="c3">ext editor</span><span class="c3">, </span><span class="c3">which usually has a blank white background</span><span class="c3">. </span><span class="c3">IDLE, short for the interactive development environment, is a program that comes packaged with Python when you install it.</span><span class="c3">&nbsp;In IDLE, you can compose your code, then save it and run it. When you run your code, a new interactive shell will open and display the results of your code. </span></li></ol><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3 c73">CAUTION</span><span class="c3">: Both shells, without telling you what the problem is, may reject code that is copied from Kindle or other sources. If you copy and paste some code and get an unexplainable error message, try typing the code.</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">You can type Python directly into the interactive shell and the shell prints the results. At the prompt (&gt;&gt;&gt;) t</span><span class="c3">ype </span></p><p class="c1"><span class="c3"><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c75 c4">print</span><span class="c4 c66">(&ldquo;Hello World&rdquo;)</span><span class="c4">&nbsp;</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">and press enter. The interactive shell will respond by printing &ldquo;Hello World.&rdquo; In Computer Science, there is a tradition that whenever you teach a new programming language, the first program you should show them is how to print &ldquo;Hello World.&rdquo; Congratulations, you just wrote your first program! </span></p><p class="c0"><span class="c3">IDLE can be used as &nbsp;a text editor like Microsoft Word, with the added benefit of being able to run the programs you write. In your computer&rsquo;s Programs menu</span><span class="c3">, select IDLE. &nbsp;The new window that pops up is where you can write code you want to save for later use. </span><span class="c3">Type</span></p><p class="c0"><span class="c3"><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c4 c75">print</span><span class="c4 c66">(&ldquo;Hello World&rdquo;)</span><span class="c4">&nbsp;</span></p><p class="c0 c9"><span class="c3"></span></p><p class="c1"><span class="c3">into the IDLE text editor (white background) and go to &ldquo;File&rdquo; and select &ldquo;Save As.&rdquo; Name your file &ldquo;hello_world.py&rdquo; and save it. &nbsp;Once you&rsquo;ve saved your file, click &ldquo;Run,&rdquo; and select &ldquo;Run Module.&rdquo; &ldquo;Hello World&rdquo; will print in the interactive shell, as if you had typed the command in yourself. </span></p><p class="c0 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.fy9uzoranqbl"><span>Running Example Programs </span></h3><p class="c0"><span class="c3">Throughout the book, I will give examples of code and</span><span class="c3">&nbsp;what happens</span><span class="c3">&nbsp;when you run it. Whenever I do this, you should type out the code and run it yourself. </span><span class="c3 c5">You should run each example from the interactive shell, unless the example has a </span><span class="c4 c5">#</span><span class="c3 c5">&nbsp;or </span><span class="c4 c5">&ldquo;&ldquo;&ldquo;</span><span class="c3 c5">, followed by a link to GitHub, in which case you should run the code from a Python file.</span><span class="c3">&nbsp;</span><span class="c3 c5">GitHub is a website used to host code, and all of the examples not meant to be run in the shell are hosted on GitHub. Copy and paste the link provided in the example into your web browser to view the code. You should type out every example yourself, but if you have to copy and paste code, copy and paste it from GitHub, not the book, otherwise it might not work.</span><sup><a href="#cmnt6" id="cmnt_ref6">[f]</a></sup><span class="c3 c5">&nbsp;</span></p><p class="c0"><span class="c3">The reason for this is that short examples are best run using the shell, whereas the text editor is better for longer programs because it lets you &nbsp;save and edit them. In the interactive shell, if you make a mistake in your code&mdash;a typo for example, and it doesn&rsquo;t work, you have to type everything all over again. With the text editor you can save your work, so if you make a mistake, you simply edit the mistake and rerun the program. Thus, the interactive shell is great for quickly running very short programs like </span><span class="c4">print(&ldquo;Hello World!&rdquo;)</span><span class="c3">&nbsp;but the text editor is much better for working on longer programs. &nbsp;</span></p><p class="c0"><span class="c3">In our example, we named our file &ldquo;hello_world.py,&rdquo; but you could have named it &ldquo;anything_youd_like.py&rdquo; and it would have worked the same. If you see an example in the book you think is a good fit to use the text editor, it is up to you to create and save a new Python file, named whatever you&rsquo;d like as long as it has the .py extension, and to run the code. &nbsp;</span></p><h2 class="c1 c13" id="h.8zsym7fgntfl"><span>Chapter </span><span class="c3">3. </span><span>Introduction to Programming</span></h2><p class="c1"><span class="c3">It</span><span class="c3">&rsquo;</span><span class="c3">s the only job I can think of where I get to be both an engineer and an artist. There</span><span class="c3">&rsquo;</span><span class="c3">s an incredible, rigorous, technical element to it, which I like because you have to do very precise thinking. On the other hand, it has a wildly creative side where the boundaries of imagination are the only real limitation. </span></p><p class="c1"><span class="c3">&mdash;</span><span class="c3">Andy Hertzfeld</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;We already learned how to </span><span class="c3">print &ldquo;Hello World</span><span class="c3">.&rdquo; Let&rsquo;s print it one million times. </span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p><p class="c1 c23"><span class="c7 c4 c5">for </span><span class="c4 c28">i </span><span class="c7 c4 c5">in </span><span class="c4 c25">range</span><span class="c4 c28">(</span><span class="c4 c20">1000001</span><span class="c4 c28">):</span></p><p class="c1 c23"><span class="c4 c28">&nbsp; &nbsp;</span><span class="c7 c4 c5">print</span><span class="c4 c28">(</span><span class="c40 c4">&quot;Hello World&quot;</span><span class="c4 c28">)</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">Your console should print out &ldquo;Hello World&rdquo; a million times! Even though you will probably never need to print &ldquo;Hello World&rdquo; a million times, I started with this example to show you how powerful programming can be as soon as possible. Can you think of any other area in life where you can something a million times so trivially? I can&rsquo;t. That is the power of programming. </span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.op6p1d9iihxk"><span>Conventions used in this Book</span></h3><p class="c0"><span class="c3">Throughout the book, you will see &ldquo;&gt;&gt;&rdquo; &nbsp;after each programming example. This represents the output of the program (printed in the interactive shell). </span><span class="c3">Ellipses (...) mean &ldquo;and so on.&rdquo; </span><span class="c3">So from here</span><span class="c3">&nbsp;</span><span class="c3">on out, the above program will be written like this:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c23"><span class="c7 c4 c5">for </span><span class="c4 c28">i </span><span class="c7 c4 c5">in </span><span class="c4 c25">range</span><span class="c4 c28">(</span><span class="c4 c20">1000001</span><span class="c4 c28">):</span></p><p class="c1 c23"><span class="c4 c28">&nbsp; &nbsp;</span><span class="c7 c4 c5">print</span><span class="c4 c28">(</span><span class="c40 c4">&quot;Hello World&quot;</span><span class="c4 c28">)</span></p><p class="c1 c23 c9"><span class="c4"></span></p><p class="c1 c23"><span class="c4">&gt;&gt; Hello World</span></p><p class="c1 c23"><span class="c4">&gt;&gt; Hello World</span></p><p class="c1 c23"><span class="c4">&gt;&gt; Hello World</span></p><p class="c1 c23"><span class="c4">&hellip; </span></p><p class="c1 c9"><span class="c4"></span></p><p class="c1"><span class="c3">with everything color-coded representing code, and everything after &ldquo;&gt;&gt;&rdquo; representing the output of the Python</span><span class="c3">&nbsp;shell. </span><span class="c3">Furthermore, anything in this book (excluding examples) written in italics represents either a Python keyword (a word in Python that does something special) or a line of Python code. So for example, if I wanted to refer to the keyword </span><span class="c15 c3">for</span><span class="c3">&nbsp;from this example, it will be italicized.</span><span class="c3">&nbsp; </span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.5zbgjmxm1lr"><span>Comments</span></h3><p class="c1"><span class="c11 c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Programmers document code by writing English in it so they remember why they did things a certain way when they come back to code they wrote in the past. This is achieved with something called comments. The Python interpreter ignores anything in front of the pound symbol, even if it would ordinarily cause an error. </span></p><p class="c1"><span class="c11 c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p><p class="c0"><span class="c4 c61"># This is a comment</span></p><p class="c0"><span class="c7 c4 c5">print</span><span class="c4 c28">(</span><span class="c40 c4">&quot;The comment does not affect this code&quot;</span><span class="c4 c28">)</span></p><p class="c1 c9"><span class="c11 c4"></span></p><p class="c1"><span class="c11 c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; The comment does not affect this code</span></p><p class="c1 c9"><span class="c11 c3"></span></p><p class="c1"><span class="c11 c3">Comments let programmers write messages about why they chose to do something in their code. However, you don&rsquo;t want to over comment. Only comment if there is a reason you are doing something unusual;</span><span class="c36 c3">&nbsp;</span><span class="c3">d</span><span class="c11 c3">o not comment anywhere close to every line of code you write. </span><span class="c11 c3">If you need to write a comment that spans more than one line, use triple strings:</span></p><p class="c1"><span class="c11 c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p><p class="c0"><span class="c11 c3">&ldquo;&ldquo;&ldquo;</span></p><p class="c0"><span class="c11 c3">This is a comment.</span></p><p class="c0"><span class="c11 c3">It can span multiple lines. </span></p><p class="c0"><span class="c11 c3">&rdquo;&rdquo;&rdquo;</span><sup><a href="#cmnt7" id="cmnt_ref7">[g]</a></sup></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.ukybmuz76w4j"><span>Python Spacing</span></h3><p class="c1"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">Looking at the previous example you will notice print(&ldquo;Hello World&rdquo;) is indented by four spaces. This is something Python is famous for, an important programming concept known as scope. We cover scope in the next chapter, but for now remember, everything you see with an indent needs to be indented 4 spaces.</span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.uvy1nhxd8msw"><span>Python as a Calculator</span></h3><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">You can use Python to do math, just like a calculator. You can add, subtract, divide, multiply, raise a number to a power and much more.</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c4 c20">2 + 2</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; 4</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c4 c20">2 </span><span class="c4 c28">- </span><span class="c4 c20">2</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; 0</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;4 / 2</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; 2</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2 * 2</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; 4</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2 ** 4</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; 16</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">The symbols we used to do these calculations</span><span class="c3">,</span><span class="c3">&nbsp;i</span><span class="c3">.</span><span class="c3">e.</span><span class="c3">,</span><span class="c3">&nbsp;</span><span class="c15 c3">+</span><span class="c3">&nbsp;and * are called </span><span class="c3">operators</span><span class="c3">. </span></p><p class="c1 c9"><span class="c3"></span></p><a id="t.d2ba8dd17089cd410405b57eff9c25ab219f295b"></a><a id="t.0"></a><table class="c31"><tbody><tr class="c65"><td class="c49" colspan="1" rowspan="1"><p class="c37"><span class="c60 c3 c5">Operator</span></p></td><td class="c26" colspan="1" rowspan="1"><p class="c37"><span class="c3 c5 c60">Operation</span></p></td><td class="c52" colspan="1" rowspan="1"><p class="c37"><span class="c60 c3 c5">Example</span></p></td><td class="c67" colspan="1" rowspan="1"><p class="c37"><span class="c3 c5">Evaluates to...</span></p></td></tr><tr class="c65"><td class="c49" colspan="1" rowspan="1"><p class="c37"><span class="c34 c3">**</span></p></td><td class="c26" colspan="1" rowspan="1"><p class="c37"><span class="c34 c3">Exponent</span></p></td><td class="c52" colspan="1" rowspan="1"><p class="c37"><span class="c34 c3">2 ** 3</span></p></td><td class="c67" colspan="1" rowspan="1"><p class="c37"><span class="c34 c3">8</span></p></td></tr><tr class="c65"><td class="c49" colspan="1" rowspan="1"><p class="c37"><span class="c3 c34">%</span></p></td><td class="c26" colspan="1" rowspan="1"><p class="c37"><span class="c34 c3">Modulo/remainder</span></p></td><td class="c52" colspan="1" rowspan="1"><p class="c37"><span class="c34 c3">22 % 8</span></p></td><td class="c67" colspan="1" rowspan="1"><p class="c37"><span class="c34 c3">6</span></p></td></tr><tr class="c65"><td class="c49" colspan="1" rowspan="1"><p class="c37"><span class="c34 c3">//</span></p></td><td class="c26" colspan="1" rowspan="1"><p class="c37"><span class="c34 c3">Integer division/floored quotient</span></p></td><td class="c52" colspan="1" rowspan="1"><p class="c37"><span class="c34 c3">22 // 8</span></p></td><td class="c67" colspan="1" rowspan="1"><p class="c37"><span class="c34 c3">2</span></p></td></tr><tr class="c65"><td class="c49" colspan="1" rowspan="1"><p class="c37"><span class="c34 c3">/</span></p></td><td class="c26" colspan="1" rowspan="1"><p class="c37"><span class="c34 c3">Division</span></p></td><td class="c52" colspan="1" rowspan="1"><p class="c37"><span class="c34 c3">22 / 8</span></p></td><td class="c67" colspan="1" rowspan="1"><p class="c37"><span class="c34 c3">2.75</span></p></td></tr><tr class="c65"><td class="c49" colspan="1" rowspan="1"><p class="c37"><span class="c34 c3">*</span></p></td><td class="c26" colspan="1" rowspan="1"><p class="c37"><span class="c34 c3">Multiplication</span></p></td><td class="c52" colspan="1" rowspan="1"><p class="c37"><span class="c34 c3">3 * 5</span></p></td><td class="c67" colspan="1" rowspan="1"><p class="c37"><span class="c34 c3">15</span></p></td></tr><tr class="c65"><td class="c49" colspan="1" rowspan="1"><p class="c37"><span class="c34 c3">-</span></p></td><td class="c26" colspan="1" rowspan="1"><p class="c37"><span class="c34 c3">Subtraction</span></p></td><td class="c52" colspan="1" rowspan="1"><p class="c37"><span class="c34 c3">5 - 2</span></p></td><td class="c67" colspan="1" rowspan="1"><p class="c37"><span class="c34 c3">3</span></p></td></tr><tr class="c65"><td class="c49" colspan="1" rowspan="1"><p class="c37"><span class="c34 c3">+</span></p></td><td class="c26" colspan="1" rowspan="1"><p class="c37"><span class="c34 c3">Addition</span></p></td><td class="c52" colspan="1" rowspan="1"><p class="c37"><span class="c34 c3">2 + 2</span></p></td><td class="c67" colspan="1" rowspan="1"><p class="c37"><span class="c34 c3">4</span></p></td></tr></tbody></table><p class="c1"><span class="c41 c3">20</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">An </span><span class="c3">operator</span><span class="c3">&nbsp;you may not be familiar with is called the </span><span class="c3">modulo</span><span class="c15 c3">&nbsp;</span><span class="c3">operator</span><span class="c3">, used to get the remainder when two numbers are divided:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2 % 2</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; 0</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;10 % 3</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; 1</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c0"><span class="c3">The order of operations Python uses is the same as math. Remember Please Excuse My Dear Aunt Sally? If not, it is an acronym for the order of operations:</span><span class="c3">&nbsp;</span><span class="c3">parentheses, exponents, multiplication, division, addition and subtraction. </span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c10"><span class="c3">2 + 2 * 2</span></p><p class="c1 c10"><span class="c3">&gt;&gt; 6</span></p><p class="c1 c9 c10"><span class="c3"></span></p><p class="c1 c10"><span class="c3">(2+2) * 2</span></p><p class="c1 c10"><span class="c3">&gt;&gt; 8</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">In the first example, 2 * 2 is calculated first, followed by 2 + 2, because multiplication takes precedence over addition. In the second example, (2+2) is evaluated first, because parentheses are always evaluated before anything else. &nbsp;</span></p><p class="c1 c9"><span class="c11 c3"></span></p><h3 class="c1 c13" id="h.h0e8cecjji2f"><span>Types</span></h3><p class="c1"><span class="c11 c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">Programming is about manipulating data, and Python separates data into different categories. So far we&rsquo;ve worked with two different types</span><span class="c3">&mdash;</span><span class="c3">integers, which are whole numbers like 2, and strings, like &ldquo;Hello world</span><span class="c3">.&rdquo;</span><span class="c3">&nbsp;Python has many more types, such as floats&mdash;which represent decimal numbers, and boolean&mdash;which represents true or false. In this section, we will review the string, int, float and boolean types. </span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Strings are a type used to represent text. Python knows something is a string when it is in quotes. You can use either single or double quotes (or triple quotes if the string takes up more than one line in the IDLE text editor). </span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">print(&ldquo;Anything in double quotes is a string.&rdquo;)</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(&lsquo;So is anything in single quotes.&rsquo;)</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(&quot;&quot;&quot;So are triple quotes, but </span></p><p class="c0 c10"><span class="c3">strings in triple quotes can span multiple lines.&quot;&quot;&quot;)</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; &ldquo;Anything in double quotes is a string.&rdquo;</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&hellip;.</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">Floats are similar to integers, but they have decimals. </span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c10"><span class="c3">2.1 + 2.2</span></p><p class="c1 c10"><span class="c3">&gt;&gt; 4.300000000000001</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">You may be wondering why the answer is incorrect (it has an extra 1 at the end). Ignore this for now, we will cover this in part IV. For now, just know when you are playing around with floats, you aren&rsquo;t losing your mind, the results are off. </span></p><p class="c0"><span class="c3">Another important type is called boolean, which represents a value of </span><span class="c15 c3">True</span><span class="c3">&nbsp;or </span><span class="c15 c3">False</span><span class="c3">. </span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c10"><span class="c3">True</span></p><p class="c1 c10"><span class="c3">&gt;&gt; True</span></p><p class="c1 c9 c10"><span class="c3"></span></p><p class="c1 c10"><span class="c3">False</span></p><p class="c1 c10"><span class="c3">&gt;&gt; False </span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">While </span><span class="c3">this</span><span class="c3">&nbsp;type may not seem useful now, it will be later on because programs often make decisions based on whether a condition is </span><span class="c15 c3">True</span><span class="c3">&nbsp;or </span><span class="c15 c3">False</span><span class="c3">. </span></p><p class="c0"><span class="c3">You may be wondering, </span><span class="c3">&ldquo;W</span><span class="c3">hat </span><span class="c3">is </span><span class="c3">the point of dividing everything up into different types</span><span class="c3">?&rdquo; When you run your program, Python uses</span><span class="c3">&nbsp;a &nbsp;program called a compiler that takes an expression like &ldquo;2 + 2&rdquo; and turns it into binary &nbsp;instructions your computer can understand. The compiler needs types in order to figure out what to do with something like &ldquo;2 + 2&rdquo;. For example, integers are not the only type that can be added together. Strings can added together as well, which is called concatenation:</span></p><p class="c0 c9"><span class="c3"></span></p><p class="c0"><span class="c3">&ldquo;Hello &rdquo; + &ldquo;World&rdquo;</span></p><p class="c0"><span class="c3">&gt;&gt; &ldquo;Hello World&rdquo;</span></p><p class="c0 c9"><span class="c3"></span></p><p class="c0"><span class="c3">2 + 2</span></p><p class="c0"><span class="c3">&gt;&gt; 4</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">If there were no types, the compiler wouldn&rsquo;t be able to add strings differently than it adds integers. We will learn more about compilers when we go over Computer Science fundamentals in part IV of this book. </span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.rqkxhrrjw8og"><span class="c36">Errors</span></h3><p class="c1"><span class="c3 c5">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">In Python, s</span><span class="c3">trings have to be surrounded by quotes; this is an example of Python&rsquo;s syntax, which are the rules of how you write a language. If </span><span class="c3">you </span><span class="c3">created your own programming language, you would need to define the syntax for your new language. For example, you could decide strings need to be surrounded by dollar signs instead of quotes</span><span class="c3">,</span><span class="c3">&nbsp;i</span><span class="c3">.</span><span class="c3">e.</span><span class="c3">,</span><span class="c3">&nbsp; </span><span class="c3">$Hello World.$. </span></p><p class="c0"><span class="c3">However, when you are using Python, you must follow its syntax. If you write a </span><span class="c3">Python </span><span class="c3">program, and disregard Python&rsquo;s syntax, you will get an error called a syntax error. Look at what happens if you try to define a string in Python with only one quote:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;my_string = &ldquo;Hello World. </span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(my_string)</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c0"><span class="c3">&gt;&gt; File &quot;/Users/coryalthoff/PycharmProjects/se.py&quot;, line 1</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;my_string = &#39;d</span></p><p class="c1"><span class="c3">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;^</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SyntaxError: EOL while scanning string literal</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">Although this error may look scary&mdash;don&rsquo;t worry, errors like this happen all the time. A</span><span class="c15 c3">&nbsp;syntax error</span><span class="c3">&nbsp;is </span><span class="c3">just one of many errors you will get </span><span class="c3">when </span><span class="c3">programming. When there is an error in your program, the Python Interpreter prints out a message like the one in the previous example. The message will tell you what file the error was in, what line it occurred on, and what kind of error it was. </span></p><p class="c0"><span class="c3">When you get an error, go to the line number the problem occurred on, and try to figure out what you did wrong. In this example, we would go to our code and look at line 1. After staring at it for a while, we would eventually notice there is only one quote. To fix it, just add a quote at the end and rerun the program. Learning to program, you will frequently get errors, but don&rsquo;t worry, they will decrease over time and you will get better at fixing them quickly. </span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Syntax errors are just one of many different types of errors you can get while programming. For example, if you try dividing by zero, you will get a</span><span class="c3">&nbsp;</span><span class="c15 c3">ZeroDivisionError</span><span class="c3">:</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;10 % 0</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; Traceback (most recent call last):</span></p><p class="c1"><span class="c3">&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; File &quot;/Users/coryalthoff/PycharmProjects/tstp/indent_error.py&quot;, line 1, in &lt;module&gt;</span></p><p class="c1"><span class="c3">&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; 10 % 0</span></p><p class="c0"><span class="c3">&nbsp; &nbsp; &nbsp; ZeroDivisionError: integer division or modulo by zero</span></p><p class="c0 c9"><span class="c3"></span></p><p class="c1"><span class="c3">And if you incorrectly indent your code, you get &nbsp;an </span><span class="c15 c3">IndentationError</span><span class="c3">:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;y = 2</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x = 1</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; &nbsp;File &quot;/Users/coryalthoff/PycharmProjects/tstp/indent_error.py&quot;, line 2</span></p><p class="c1"><span class="c3">&nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;x = 1</span></p><p class="c1"><span class="c3">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ^</span></p><p class="c1"><span class="c3">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; IndentationError: unexpected indent</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">Remember, when you run into an error, go to the line where the problem occurred and stare at it until you figure out the solution</span><span class="c3">&nbsp;</span><span class="c3">(or google it). </span></p><h3 class="c1 c13" id="h.oato4pnccvu"><span>Variables</span></h3><p class="c1"><span class="c3">Python lets you save data in variables, just like in math:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;b = 100</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;b</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; &ldquo;100&rdquo;</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">You can also change the value of a variable:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x = 100</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x = 200</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; 100</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; 200</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">You can add two variables as well:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x = 10</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;y &nbsp;= 10</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;z = x + y </span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;z</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; 20</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">When you have a variable and you want to increase or decrease its value (known as incrementing or decrementing), you can do it by assigning the variable to itself plus or minus the amount you are incrementing/decrementing by:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x = 10</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x = x + 1</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; 11</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x = 10</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x = 10 -1</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; 9</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">These examples are perfectly valid, however, there is a shorter syntax for incrementing and decrementing variables that you should use instead:</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x = 10</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x += 1</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; 11</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x = 10</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x -= 1</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; 9</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">You can name variables whatever you&rsquo;d like, as long as you follow three rules:</span></p><ol class="c45 lst-kix_9uwjp0g1busc-0 start" start="1"><li class="c1 c10 c38"><span class="c3">Variables can&rsquo;t have spaces. If you want to use two words in a variable, you should put an underscore in between: i</span><span class="c3">.</span><span class="c3">e</span><span class="c3">,</span><span class="c3">. my_variable = &ldquo;A string!&rdquo;</span></li><li class="c1 c10 c38"><span class="c3">You can only name variables using letters, numbers and the underscore symbol.</span></li><li class="c1 c10 c38"><span class="c3">You cannot start a variable name with a number. While you can start a variable with an </span><span class="c3">underscore, it does have a special meaning we will cover later</span><span class="c3">, but avoid it for now.</span></li></ol><p class="c1 c10"><span class="c3">Python has something called built-in keywords&mdash;words with special meaning built into the Python programming language. def is an example of a built-in keyword used to define a function, which we will cover in the next chapter. You cannot use built in keywords as variable names. The following code will run fine:</span></p><p class="c1 c9 c10"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;definition = &ldquo;A string.&rdquo;</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">Whereas this will cause an error because it uses a built-in keyword &nbsp;</span><span class="c15 c3">def</span><span class="c3">&nbsp;as a</span><span class="c3">&nbsp;variable name:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c0 c10"><span class="c3">def = &ldquo;A string.&rdquo; </span></p><p class="c0 c9 c10"><span class="c3"></span></p><p class="c1 c10"><span class="c3">You can find a list of built-in keywords here: http://zetcode.com/lang/python/keywords/</span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.3zmxfgapcgqu"><span>Comparison Operators</span></h3><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">In order to write interesting programs, we need to be able to check for certain conditions, which we can do with comparison operators. Comparison operators check for truthiness</span><span class="c3">,</span><span class="c3">&nbsp;that is whether something evaluates to True or False</span><span class="c3">,</span><span class="c3">&nbsp;which we learned earlier is the type </span><span class="c3">boolean</span><span class="c3">.</span><span class="c3">&nbsp; &nbsp; &nbsp;</span></p><p class="c0"><span class="c3">Greater than (&gt;) and less than (&lt;) are examples of comparison operators. Greater than checks if the number on the right is bigger than the number on the left:</span></p><p class="c0 c9"><span class="c3"></span></p><p class="c1 c10"><span class="c3">100 &gt; 10</span></p><p class="c0"><span class="c3">&gt;&gt; True</span></p><p class="c0 c9"><span class="c3"></span></p><p class="c1"><span class="c3">While less than checks if the number on the left is smaller than the number on the right:</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p><p class="c1 c10"><span class="c3">100 &lt; 10</span></p><p class="c1 c10"><span class="c3">&gt;&gt; False</span></p><p class="c1 c9 c10"><span class="c3"></span></p><p class="c1"><span class="c3">Adding an equal sign to greater than creates a new comparison operator, greater than or equal to:</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1 &lt;= 2</span></p><p class="c1 c10"><span class="c3">&gt;&gt; True</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">and adding an equal sign to less than creates a new comparison operator less than or equal to:</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2 &gt;= 2</span></p><p class="c1 c10"><span class="c3">&gt;&gt; True</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">To check for equality, use the </span><span class="c3">comparison operator =</span><span class="c15 c3">=</span><span class="c3">.</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c10"><span class="c3">1 == 2</span></p><p class="c1 c10"><span class="c3">&gt;&gt; False</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c10"><span class="c3">2 == 2</span></p><p class="c1 c10"><span class="c3">&gt;&gt; True</span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.do265k9aix3t"><span>Assignment vs. Equality</span></h3><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">Earlier, we assigned variables to numbers, like &nbsp;</span><span class="c15 c3">x = 100</span><span class="c3">. It may be tempting to read in your head &ldquo;x equals 100&rdquo;, but don&rsquo;t. As we saw earlier, in Python, = &nbsp;is the assignment operator. It is used to assign a value to a variable, not to check for equality. When you see </span><span class="c15 c3">x= 100</span><span class="c3">, you should think &ldquo;x gets one hundred&rdquo;. The comparison operator &nbsp;</span><span class="c15 c3">==</span><span class="c3">&nbsp; is used to check for equality so if you see </span><span class="c15 c3">x == 100</span><span class="c3">, then you should think &ldquo;x equals 100&rdquo;. </span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.cptodeqn1w4w"><span>And Or</span></h3><p class="c0"><span class="c11 c3">The keywords </span><span class="c11 c15 c3">and</span><span class="c11 c3">&nbsp;and </span><span class="c11 c15 c3">or</span><span class="c11 c3">&nbsp;check the truthiness of multiple conditions. </span><span class="c11 c15 c3">and</span><span class="c11 c3">&nbsp;will return the boolean type </span><span class="c11 c15 c3">True</span><span class="c11 c3">&nbsp;only if both conditions are </span><span class="c11 c15 c3">True</span><span class="c11 c3">, whereas </span><span class="c11 c15 c3">or</span><span class="c11 c3">&nbsp;will return </span><span class="c11 c15 c3">True</span><span class="c11 c3">&nbsp;if either of the conditions are </span><span class="c11 c15 c3">True</span><span class="c11 c3">:</span></p><p class="c1 c9"><span class="c11 c3"></span></p><p class="c1"><span class="c11 c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x = 1</span></p><p class="c1"><span class="c11 c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;y = 2</span></p><p class="c1 c9"><span class="c11 c3"></span></p><p class="c1"><span class="c11 c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x == 1 and y == 2</span></p><p class="c1"><span class="c11 c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; True</span></p><p class="c1 c9"><span class="c11 c3"></span></p><p class="c1"><span class="c11 c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x == 1 and y == 3</span></p><p class="c1"><span class="c11 c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; False</span></p><p class="c1 c9"><span class="c11 c3"></span></p><p class="c1"><span class="c11 c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x == 1 or y == 1</span></p><p class="c1"><span class="c11 c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; True</span></p><p class="c1"><span class="c11 c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p><p class="c1"><span class="c11 c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x == 3 or y == 3</span></p><p class="c1"><span class="c11 c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; False</span></p><p class="c1 c9"><span class="c11 c3"></span></p><p class="c1"><span class="c11 c3">You can compare as many conditions as you&rsquo;d like:</span></p><p class="c1"><span class="c11 c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p><p class="c1"><span class="c11 c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x == 1 and y == 2 and 1 % 1 == 0 and &nbsp;2 - 1 != 5</span></p><p class="c1"><span class="c11 c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; True</span></p><p class="c1"><span class="c11 c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p><p class="c1"><span class="c11 c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x == 5 or x == 4 or x == 3 or x == 2 or x == 1</span></p><p class="c1"><span class="c11 c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; True </span></p><p class="c1 c9"><span class="c11 c3"></span></p><p class="c1"><span class="c11 c3">The &nbsp;built-in keyword </span><span class="c11 c15 c3">not</span><span class="c11 c3">&nbsp;turns the boolean type True to the boolean type </span><span class="c11 c15 c3">False</span><span class="c11 c3">&nbsp;and </span><span class="c11 c15 c3">False</span><span class="c11 c3">&nbsp;to </span><span class="c11 c15 c3">True</span><span class="c11 c3">:</span></p><p class="c1 c9"><span class="c11 c3"></span></p><p class="c1"><span class="c11 c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x = 1</span></p><p class="c1"><span class="c11 c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;y = 2</span></p><p class="c1 c9"><span class="c11 c3"></span></p><p class="c1"><span class="c11 c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;not x == 1</span></p><p class="c1"><span class="c11 c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; False</span></p><p class="c1"><span class="c11 c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p><p class="c1"><span class="c11 c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;not x == 1 and y ==2</span></p><p class="c1"><span class="c11 c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; False </span></p><p class="c1 c9"><span class="c11 c3"></span></p><p class="c1"><span class="c11 c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;not x == 3 and y == 4</span></p><p class="c1"><span class="c11 c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; True</span></p><p class="c1 c9"><span class="c11 c3"></span></p><p class="c1"><span class="c11 c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;not x == 1 or x == 3</span></p><p class="c1"><span class="c11 c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; False</span></p><p class="c1 c9"><span class="c11 c3"></span></p><p class="c1"><span class="c11 c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;not x == 5 or x == 10</span></p><p class="c1"><span class="c11 c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; True </span></p><h3 class="c1 c13" id="h.ukl83ir4f1nj"><span>Control Flow</span></h3><p class="c1"><span class="c3 c5">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">We can use comparison operators with something called control flow to allow our programs to respond to different conditions</span><span class="c3">. For example, print(&ldquo;Hello America!&rdquo;) will always print &ldquo;Hello America!&rdquo;. What if we want to print &ldquo;Hello America!&rdquo; or &ldquo;Hello Canada!&rdquo;, depending on the truthiness of some condition? We can do this with control flow using the keywords </span><span class="c15 c3">if</span><span class="c3">, </span><span class="c15 c3">elif</span><span class="c3">&nbsp;and </span><span class="c15 c3">else</span><span class="c3">.</span></p><p class="c1 c9"><span class="c36 c3"></span></p><p class="c1 c10"><span class="c3">&ldquo;&ldquo;&ldquo;</span></p><p class="c1 c10"><span class="c17 c3"><a class="c18" href="https://www.google.com/url?q=https://github.com/calthoff/tstp/blob/master/part_I/introduction_to_programming/control_flow_ex1.py&amp;sa=D&amp;ust=1467337425965000&amp;usg=AFQjCNGQQVtTLI2NzYJzTxa7a34RHp6LNw">https://github.com/calthoff/tstp/blob/master/part_I/introduction_to_programming/control_flow_ex1.py</a></span></p><p class="c1 c10"><span class="c3">&rdquo;&rdquo;&rdquo;</span></p><p class="c1 c9 c10"><span class="c3"></span></p><p class="c1"><span class="c36 c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c36 c3">n</span><span class="c3 c70">ationality </span><span class="c36 c3">= &ldquo;America&rdquo;</span></p><p class="c1"><span class="c36 c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if nationality == &ldquo;America&rdquo;:</span></p><p class="c1"><span class="c36 c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; </span><span class="c3 c70">print</span><span class="c36 c3">(&ldquo;Hello America!&rdquo;)</span></p><p class="c1"><span class="c36 c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3 c103">if</span><span class="c36 c3">&nbsp;nationality == &ldquo;Canada&rdquo;:</span></p><p class="c1"><span class="c3 c36">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; </span><span class="c3 c96">print</span><span class="c36 c3">(&ldquo;Hello Canada!&rdquo;)</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; America</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">In this example, we introduced a new built-in keyword&mdash;</span><span class="c15 c3">if</span><span class="c3">. We can combine </span><span class="c15 c3">if</span><span class="c3">&nbsp;with a comparison operator to do something only if a condition is </span><span class="c15 c3">True</span><span class="c3">. In this case, we use the equality operator to print &ldquo;Hello America!&rdquo; only if the variable nationality is equal to &ldquo;America&rdquo;. Note that after each if statement, there are four spaces. This is an important concept in programming called scope, which we cover in the next chapter. For now, just remember that the line after every </span><span class="c15 c3">if</span><span class="c3">, </span><span class="c15 c3">elif</span><span class="c3">&nbsp;and </span><span class="c15 c3">else</span><span class="c3">&nbsp;statement needs to be indented by four spaces.</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">If we change the variable nationality to &ldquo;Canada&rdquo;, our program will now print &ldquo;Hello Canada!&rdquo;.</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c10"><span class="c3">&ldquo;&ldquo;&ldquo;</span></p><p class="c1 c10"><span class="c3">https://github.com/calthoff/tstp/blob/master/part_I/introduction_to_programming/control_flow_ex2.py</span></p><p class="c1 c10"><span class="c3">&rdquo;&rdquo;&rdquo;</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p><p class="c0"><span class="c3">nationality = &ldquo;Canada&rdquo;</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if nationality == &ldquo;America&rdquo;:</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(&ldquo;Hello America!&rdquo;)</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if nationality == &ldquo;Canada&rdquo;:</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(&ldquo;Hello Canada!&rdquo;)</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; Canada</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">And if we change nationality to anything else, nothing will happen. </span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c10"><span class="c3">&ldquo;&ldquo;&ldquo;</span></p><p class="c1 c10"><span class="c3">https://github.com/calthoff/tstp/blob/master/part_I/introduction_to_programming/control_flow_ex3.py</span></p><p class="c1 c10"><span class="c3">&rdquo;&rdquo;&rdquo;</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nationality = &ldquo;Indonesia&rdquo;</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if nationality == &ldquo;America&rdquo;:</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(&ldquo;Hello America!&rdquo;)</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if nationality == &ldquo;Canada&rdquo;:</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(&ldquo;Hello Canada!&rdquo;)</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; </span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">Instead of doing nothing, we can easily print a third string, for every other nationality using the keyword </span><span class="c15 c3">else</span><span class="c3">. </span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c10"><span class="c3">&ldquo;&ldquo;&ldquo;</span></p><p class="c1 c10"><span class="c3">https://github.com/calthoff/tstp/blob/master/part_I/introduction_to_programming/control_flow_ex4.py</span></p><p class="c1 c10"><span class="c3">&rdquo;&rdquo;&rdquo;</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nationality = &ldquo;Indonesia&rdquo;</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if nationality == &ldquo;America&rdquo;:</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(&ldquo;Hello America!&rdquo;)</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else:</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(&ldquo;Hello world!&rdquo;)</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p><p class="c1"><span class="c3">&gt;&gt; Hello World! </span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">An </span><span class="c15 c3">else</span><span class="c3">&nbsp;must be preceded by an </span><span class="c15 c3">if</span><span class="c3">. We can have as many </span><span class="c15 c3">if</span><span class="c3">s as we want in a row, but we can only have one </span><span class="c15 c3">else</span><span class="c3">&nbsp;per </span><span class="c15 c3">if</span><span class="c3">. However, you can have multiple </span><span class="c15 c3">elifs</span><span class="c3">&nbsp;(which stands for else if) in a row, as long as they are preceded by an </span><span class="c15 c3">if</span><span class="c3">. </span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c10"><span class="c3">&ldquo;&ldquo;&ldquo;</span></p><p class="c1 c10"><span class="c3">https://github.com/calthoff/tstp/blob/master/part_I/introduction_to_programming/control_flow_ex5.py</span></p><p class="c1 c10"><span class="c3">&rdquo;&rdquo;&rdquo;</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nationality = &ldquo;Indonesia&rdquo;</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if nationality == &ldquo;America&rdquo;:</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(&ldquo;Hello America!&rdquo;)</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;elif nationality == &ldquo;Canada&rdquo;:</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(&ldquo;Hello Canada!&rdquo;)</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;elif nationality == &ldquo;Thailand&rdquo;:</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(&ldquo;Hello Thailand!&rdquo;)</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;elif nationality == &ldquo;Mexico&rdquo;:</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(&ldquo;Hello Mexico!&rdquo;)</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else:</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(&ldquo;Hello world!&rdquo;)</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p><p class="c1"><span class="c3">&gt;&gt; Hello World! </span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">In this program, Python will evaluate each of these statements in order, one at a time, stopping either when one of them is </span><span class="c15 c3">True</span><span class="c3">, or it goes through all of them. &nbsp;Here is another example:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c10"><span class="c3">&ldquo;&ldquo;&ldquo;</span></p><p class="c1 c10"><span class="c3">https://github.com/calthoff/tstp/blob/master/part_I/introduction_to_programming/control_flow_ex6.py</span></p><p class="c1 c10"><span class="c3">&rdquo;&rdquo;&rdquo;</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x = 10000</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if x == 97:</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(97)</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;elif x == 98:</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print (98)</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;elif x == 99:</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(99)</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;elif x == 100:</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(100)</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;elif x == 101:</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(101)</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else:</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(</span><span class="c3">&ldquo;Number </span><span class="c3">not found!&rdquo;)</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; </span><span class="c3">Number </span><span class="c3">not found!</span></p><h3 class="c1 c13" id="h.yej81baz0oo2"><span>None</span></h3><p class="c0"><span class="c15 c3">None</span><span class="c3">&nbsp;is a built-in constant (the value is always the same) like </span><span class="c15 c3">True</span><span class="c3">&nbsp;and </span><span class="c15 c3">False</span><span class="c3">. </span><span class="c15 c3">None</span><span class="c3">&nbsp;is used to represent the absence of a value:</span></p><p class="c0 c9"><span class="c3"></span></p><p class="c0"><span class="c3">x = 10</span></p><p class="c0"><span class="c3">x = None</span></p><p class="c0"><span class="c3">print(x)</span></p><p class="c0 c9"><span class="c3"></span></p><p class="c0"><span class="c3">&gt;&gt; None</span></p><p class="c1 c9"><span class="c36 c3"></span></p><p class="c1"><span class="c3">We can test if a variable is </span><span class="c15 c3">None</span><span class="c3">&nbsp;using control flow.</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c10"><span class="c3">&ldquo;&ldquo;&ldquo;</span></p><p class="c1 c10"><span class="c3">https://github.com/calthoff/tstp/blob/master/part_I/introduction_to_programming/none_ex1.py</span></p><p class="c1 c10"><span class="c3">&rdquo;&rdquo;&rdquo;</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x = None</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if x:</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; print(&ldquo;x is not None&rdquo;)</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else:</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; print(&ldquo;x is None :( &ldquo;)</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; x is None :( </span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.rkottose9n2"><span>input</span></h3><p class="c0"><span class="c3">We can use the built</span><span class="c3">-</span><span class="c3">in </span><span class="c15 c3">input()</span><span class="c3">&nbsp;function to collect information from the person using our program. I explain how functions work in the next section, so don&rsquo;t worry it for now. &nbsp;</span></p><p class="c0 c9"><span class="c3"></span></p><p class="c1 c10"><span class="c3">&ldquo;&ldquo;&ldquo;</span></p><p class="c1 c10"><span class="c3">https://github.com/calthoff/tstp/blob/master/part_I/introduction_to_programming/input_ex1.py</span></p><p class="c1 c10"><span class="c3">&rdquo;&rdquo;&rdquo;</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c10"><span class="c3">age = input(&quot;How old are you?&quot;)</span></p><p class="c1 c10"><span class="c3">age = int(age)</span></p><p class="c1 c10"><span class="c3">if age &lt; 21:</span></p><p class="c1 c10"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(&quot;You are young!&quot;)</span></p><p class="c1 c10"><span class="c3">else:</span></p><p class="c1 c10"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(&quot;Wow you are old!&quot;)</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">We start by saving the number our user types into the variable age using </span><span class="c15 c3">input()</span><span class="c3">. Then we change the input from an integer to a string. The reason we have to do this is because </span><span class="c4">input()</span><span class="c3">collects data as a </span><span class="c4">str</span><span class="c3">, and we want our data to be an </span><span class="c4">int</span><span class="c3">&nbsp;so we can compare it to other ints (this is covered in more detail later). Next we use control flow to decide which message gets printed, depending on what the user types. If the user type a number less than 21, &ldquo;You are young!&rdquo; prints, and if the user types a number greater than 21, &ldquo;Wow you are old!&rdquo; prints. Try rerunning the program and entering 18 to see what happens. </span></p><h3 class="c1 c13" id="h.u2xh8xe84rt9"><span>Exception Handling</span></h3><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;When you rely on user input from &ldquo;input&rdquo;, you do not control the input to your program&mdash;the user does, and that input could cause an error. For example,</span><span class="c3">&nbsp;</span><span class="c3">say we write a program to accept two numbers &ldquo;a&rdquo; and &ldquo;b&rdquo; from a user, and print out the result of &ldquo;a&rdquo; divided by &ldquo;b&rdquo;.</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c10"><span class="c3">&ldquo;&ldquo;&ldquo;</span></p><p class="c1 c10"><span class="c3">https://github.com/calthoff/tstp/blob/master/part_I/introduction_to_programming/exception_handling_ex1.py</span></p><p class="c1 c10"><span class="c3">&rdquo;&rdquo;&rdquo;</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a = input(&ldquo;type a number&rdquo;)</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;b = </span><span class="c3">input(</span><span class="c3">&ldquo;type another number&rdquo;)</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a = int(a)</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;b = int(b)</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(a / b)</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; type a number</span></p><p class="c0"><span class="c3">&gt;&gt; 10</span></p><p class="c1 c10"><span class="c3">&gt;&gt; type another number</span></p><p class="c1 c10"><span class="c3">&gt;&gt; 5</span></p><p class="c1 c9 c10"><span class="c3"></span></p><p class="c1 c10"><span class="c3">&gt;&gt; 2</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p><p class="c1"><span class="c3">Our program appears to work just fine. However, we will run into a problem if the user inputs </span><span class="c4">0</span><span class="c3">&nbsp;for </span><span class="c4">b</span><span class="c3">:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a = input(&ldquo;type a number&rdquo;)</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;b = input(&ldquo;type another number&rdquo;)</span></p><p class="c1"><span class="c3">&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a = int(a)</span></p><p class="c1"><span class="c3">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;b = int(b)</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(a / b)</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; type a number</span></p><p class="c0"><span class="c3">&gt;&gt; 10</span></p><p class="c1 c10"><span class="c3">&gt;&gt; type another number</span></p><p class="c1 c10"><span class="c3">&gt;&gt; 0</span></p><p class="c1 c9 c10"><span class="c3"></span></p><p class="c1 c10"><span class="c3">&gt;&gt; Traceback (most recent call last):</span></p><p class="c1 c10"><span class="c3">&nbsp; File &quot;/Users/coryalthoff/PycharmProjects/tstp/exception_handling.py&quot;, line 3, in &lt;module&gt;</span></p><p class="c1 c10"><span class="c3">&nbsp; &nbsp; print(a / b)</span></p><p class="c1 c10"><span class="c3">ZeroDivisionError: integer division or modulo by zero</span></p><p class="c1 c9 c10"><span class="c3"></span></p><p class="c1"><span class="c3">Our program works great&mdash;until the user decides to enter &ldquo;0&rdquo; for b, in which case it will crash. We cannot allow people to use this program and just hope they will not enter zero for b. One way to solve this is to use something called exception handling, which allows Python to &ldquo;catch&rdquo; errors if they occur. In Python, the built-in keywords &ldquo;try&rdquo; and &ldquo;except&rdquo; are used for exception handling. We can change our program to use exception handling so a user entering &ldquo;0&rdquo; for b doesn&rsquo;t break our program and prints a message telling them not to enter &ldquo;0&rdquo; instead</span><span class="c3">. </span><span class="c36 c3">&nbsp;</span><span class="c3">(Note that once again the lines after </span><span class="c15 c3">except</span><span class="c3">&nbsp;and </span><span class="c15 c3">try</span><span class="c3">&nbsp;need to be indented by four spaces.)</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c0"><span class="c3">&ldquo;&ldquo;&ldquo;</span></p><p class="c1 c10"><span class="c17 c3"><a class="c18" href="https://www.google.com/url?q=https://github.com/calthoff/tstp/blob/master/part_I/introduction_to_programming/exception_handling_ex2.py&amp;sa=D&amp;ust=1467337426015000&amp;usg=AFQjCNF2JiqLnTvUUK_yOQRfo5_Iq2kitA">https://github.com/calthoff/tstp/blob/master/part_I/introduction_to_programming/exception_handling_ex2.py</a></span></p><p class="c0"><span class="c3">&rdquo;&rdquo;&rdquo;</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a = input(&ldquo;type a number&rdquo;)</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;b = input(&ldquo;type another number&rdquo;)</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;try:</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; print(a / b)</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;except ZeroDivisionError:</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; print(&ldquo;b cannot be zero. Try again.&rdquo;)</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c0"><span class="c3">&gt;&gt; type a number</span></p><p class="c0"><span class="c3">&gt;&gt; 10</span></p><p class="c1 c10"><span class="c3">&gt;&gt; type another number</span></p><p class="c1 c10"><span class="c3">&gt;&gt; 0</span></p><p class="c1 c10"><span class="c3">&gt;&gt; &ldquo;b cannot be zero. Try again.&rdquo;</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&ldquo;ZeroDivisionError&rdquo; is called a built-in exception in Python. Python has built-in exceptions for the different problems that can occur while you are programming. &ldquo;ZeroDivisionError&rdquo; occurs when you try to divide by zero, and &ldquo;SyntaxError&rdquo; occurs when your Python syntax is incorrect. You can see the full list of built-in exceptions here: https://docs.python.org/3/library/exceptions.html. You can put any of these exceptions after &ldquo;try&rdquo; and if the exception occurs, Python will execute the code in &ldquo;except&rdquo; instead of crashing. </span></p><p class="c1 c9"><span class="c11 c3"></span></p><h3 class="c1 c13" id="h.lbokdgqkw7iu"><span>Wrapping Up</span></h3><p class="c1"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">Congratulations! You now understand some of the fundamentals of programming. If this chapter is the first time you&rsquo;ve been introduced to programming, it is a lot to take in. If it was difficult to understand, don&rsquo;t worry about it! It&rsquo;s difficult for everyone. Consider re-reading this chapter and typing in all of the examples in your IDE again. It takes a while to deeply understand some of these concepts, but don&rsquo;t worry you will get the hang of it. &nbsp;</span></p><p class="c1 c9"><span></span></p><h3 class="c1 c13" id="h.50y00mqvakgy"><span>Challeng</span><span>e</span></h3><p class="c1"><span class="c3">Write a program that asks the user their age, and uses it to recommend a song they might like. </span></p><p class="c1 c9"><span></span></p><h2 class="c1 c13" id="h.69xm4mlmvzd"><span>Chapter 4. </span><span>Functions </span></h2><p class="c1"><span class="c3 c55 c33">&ldquo;</span><span class="c3 c55 c33">Functions should do one thing. They should do it well. They should do it only.&rdquo;</span></p><p class="c1"><span class="c3 c55 c33">&mdash; Robert C. Martin</span></p><p class="c1 c9"><span class="c3 c55 c33"></span></p><p class="c1 c9"><span class="c3 c33 c55"></span></p><p class="c0"><span class="c3">The first program we wrote printed &ldquo;Hello World&rdquo; using the </span><span class="c15 c3">print()</span><span class="c3">&nbsp;function</span><span class="c3">. Functions in Python work the same as functions in math. If you don&rsquo;t remember functions from algebra, here is an example:</span></p><p class="c0 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# this is not Python code </span></p><p class="c0"><span class="c4">f(x) = x * 2</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">The left half of the equation names the function f and says it takes one parameter&mdash;x. A parameter is data you pass into a function when its called. Calling a function means passing in the data the function needs and calculating the answer. The right half of the equation is the definition of the function used in conjunction with the passed in parameter to calculate an answer. When you are on the right side of the equation, x is assumed to already have been passed in by the person calling the function. In this case, our function takes x and multiplies it by two. If we call the function with 4 we get the following result:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# this is not Python code</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c4">f(4) </span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; 8</span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.pa7lyv1dow1t"><span>Defining Functions</span></h3><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">To create a function in Python, we choose a function name, decide the parameters it takes (if any), and define what the function will do. We use the following syntax:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c4">def [function_name]([parameter1]):</span></p><p class="c1"><span class="c4">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; [function_definition]</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">When I use brackets with a word in them in an example, the word describes the thing you should substitute the brackets for. Notice the function definition must be indented by four spaces because everything indented four spaces after the function is in its scope.</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;The mathematical function f(x) = x * 2 written in Python looks like this:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c0"><span class="c3"># </span><span class="c17 c3"><a class="c18" href="https://www.google.com/url?q=https://github.com/calthoff/tstp/blob/master/part_I/functions/df_ex1.py&amp;sa=D&amp;ust=1467337426027000&amp;usg=AFQjCNGDjoV02-yfpFFobjC05wp08fdDrw">https://github.com/calthoff/tstp/blob/master/part_I/functions/df_ex1.py</a></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c4">def f(x):</span></p><p class="c1 c10"><span class="c4">&nbsp; &nbsp; return x * 2</span></p><p class="c1 c9 c10"><span class="c4"></span></p><p class="c1 c10"><span class="c4">&gt;&gt;</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c15 c3">def</span><span class="c3">&nbsp;is a built-in keyword used to define a function. When you use it, Python knows you are about to define a function. After </span><span class="c15 c3">def</span><span class="c3">, you can name your function whatever you&rsquo;d like. After you name a function you have to put parentheses after it. Inside the parenthesis, you put your parameter(s). In our function definition we used the built-in keyword </span><span class="c15 c3">return</span><span class="c3">. We use this when we want our function to return a value (if left out it returns </span><span class="c15 c3">None</span><span class="c3">&nbsp;by default). To better explain this, we need to look at how we call a function. When we call a function, we use the syntax function_name([parameters]...), which is a complicated way of saying:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c0"><span class="c3"># https://github.com/calthoff/tstp/blob/master/part_I/functions/df_ex2.py</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def f(x):</span></p><p class="c1 c10"><span class="c4">&nbsp; &nbsp; return x * 2</span></p><p class="c1 c9 c10"><span class="c4"></span></p><p class="c1 c10"><span class="c4">result = f(2)</span></p><p class="c1 c10"><span class="c4">print(result)</span></p><p class="c1 c9 c10"><span class="c4"></span></p><p class="c1 c10"><span class="c4">&gt;&gt; 4</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">As you can see, our function body has access to the parameters passed into our function. In this case, the variable </span><span class="c15 c3">x</span><span class="c3">&nbsp;is assigned the value 2, because we passed in </span><span class="c15 c3">2</span><span class="c3">&nbsp;as a &nbsp;parameter when we called our function</span><span class="c3">,</span><span class="c3">&nbsp;i</span><span class="c3">.</span><span class="c3">e. </span><span class="c15 c3">f(2)</span><span class="c3">. Functions can also accept more than one parameter. The following is a valid function:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c0"><span class="c3"># https://github.com/calthoff/tstp/blob/master/part_I/functions/df_ex4.py</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c10"><span class="c4">def f(x, y, z):</span></p><p class="c1 c10"><span class="c4">&nbsp; &nbsp; </span><span class="c4">return x + y + z</span></p><p class="c1 c9"><span class="c4"></span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;result = f(1, 2, 3)</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(result)</span></p><p class="c1 c9"><span class="c4"></span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; 6</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">Similarly, you can also write a function without any parameters. &nbsp;</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c0"><span class="c3"># </span><span class="c17 c3"><a class="c18" href="https://www.google.com/url?q=https://github.com/calthoff/tstp/blob/master/part_I/functions/df_ex5.py&amp;sa=D&amp;ust=1467337426035000&amp;usg=AFQjCNF668vJjM7Kc7VnQjMzFQvG2ExcZQ">https://github.com/calthoff/tstp/blob/master/part_I/functions/df_ex5.py</a></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c4">def f():</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; return 1 + 1</span></p><p class="c1 c9"><span class="c4"></span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;result = f()</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(result)</span></p><p class="c1 c9"><span class="c4"></span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; 2</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">As I mentioned earlier, all of our function definitions are indented by four spaces. We discussed the idea of scope in the first chapter. The four spaces lets Python know that the definition belongs to our </span><span class="c15 c3">f </span><span class="c3">function. To further review this idea, what if we have a piece of code like this:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c0"><span class="c3"># </span><span class="c17 c3"><a class="c18" href="https://www.google.com/url?q=https://github.com/calthoff/tstp/blob/master/part_I/functions/df_ex5.py&amp;sa=D&amp;ust=1467337426038000&amp;usg=AFQjCNFl5ssfxM8EnpTuTSbzkUI0R4s-mQ">https://github.com/calthoff/tstp/blob/master/part_I/functions/df_ex6.py</a></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c4">def f():</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; print(&ldquo;This is part of the function f.&rdquo;)</span></p><p class="c0"><span class="c4">print(&ldquo;this is not part of function f&rdquo;)</span></p><p class="c0"><span class="c4">f()</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; </span><span class="c4">&ldquo;This is part of the function f.&rdquo;</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; &ldquo;this is not part of function f&rdquo;</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">How is Python supposed to know what code is part of the function definition, and which code is not? The answer is anything both following </span><span class="c15 c3">def f(x)</span><span class="c3">&nbsp;indented by four spaces is part of the function definition, whereas anything following it and not four spaces to the right, is not.</span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.xryixwf2klxq"><span>Optional </span><span>Parameters</span></h3><p class="c0"><span class="c3">When you are defining a function, there are two different types of parameters you can use. The kind we&rsquo;ve used so far are required parameters. These are parameters that when defined, have to be passed to the function. There are also optional parameters, that let the caller of the function pass in a parameter if they&rsquo;d like, but they don&rsquo;t have to. This is how you define an optional parameter:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c0"><span class="c3"># </span><span class="c17 c3"><a class="c18" href="https://www.google.com/url?q=https://github.com/calthoff/tstp/blob/master/part_I/functions/op_ex1.py&amp;sa=D&amp;ust=1467337426071000&amp;usg=AFQjCNG15AaGHwdeydY_VYoInSot6qP06g">https://github.com/calthoff/tstp/blob/master/part_I/functions/op_ex1.py</a></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c0"><span class="c4">def f(x=10):</span></p><p class="c0"><span class="c4">&nbsp; &nbsp; if x == 10:</span></p><p class="c0"><span class="c4">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;print(&ldquo;x is ten&rdquo;)</span></p><p class="c0"><span class="c4">&nbsp; &nbsp; else:</span></p><p class="c0"><span class="c4">&nbsp; &nbsp; &nbsp; &nbsp; print(&ldquo;x is not ten&rdquo;)</span></p><p class="c0 c9"><span class="c4"></span></p><p class="c0"><span class="c4">f()</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;f(2)</span></p><p class="c1 c9"><span class="c4"></span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; &lsquo;x is ten&rsquo;</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; &lsquo;x is not ten&rsquo;</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">Using = to give a parameter a default value lets the user change the value of the parameter if they want to, but they do not have to. </span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.j2jsngtzpj5i"><span>Pass</span></h3><p class="c0"><span class="c3">We can use the keyword </span><span class="c15 c3">pass</span><span class="c3">, which in Python means &ldquo;do nothing&rdquo; to make a simple function that does not do anything:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c10"><span class="c4">&rdquo;&rdquo;&rdquo; </span><span class="c17 c4"><a class="c18" href="https://www.google.com/url?q=https://github.com/calthoff/tstp/blob/master/part_I/functions/pass_ex1.py&amp;sa=D&amp;ust=1467337426077000&amp;usg=AFQjCNG1u1poqbb2UkhO7Rffux3-9UGNaA">https://github.com/calthoff/tstp/blob/master/part_I/functions/pass_ex1.py</a></span></p><p class="c1 c10"><span class="c4">&rdquo;&rdquo;&rdquo;</span></p><p class="c1 c9"><span class="c4"></span></p><p class="c1 c10"><span class="c4">def f():</span></p><p class="c1 c10"><span class="c4">&nbsp; &nbsp; pass</span></p><p class="c1 c9 c10"><span class="c4"></span></p><p class="c1 c10"><span class="c4">f()</span></p><p class="c1 c10"><span class="c4">&gt;&gt;</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">The pass keyword will come in handy whenever you want to write a function, but define it later.</span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.5jv8nsehe0wg"><span>Scope</span></h3><p class="c1"><span class="c3 c5">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">The keywords </span><span class="c15 c3">if</span><span class="c3">, </span><span class="c15 c3">elif</span><span class="c3">, </span><span class="c15 c3">else</span><span class="c3">, </span><span class="c15 c3">try</span><span class="c3">&nbsp;and </span><span class="c15 c3">except</span><span class="c3">&nbsp;are always followed by a colon, and the next statement is on a new line indented by four spaces. As I mentioned earlier, this is because of a programming concept called scope. Scope refers to the access to variables your program has. Let&rsquo;s look at an example:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c0"><span class="c3"># https://github.com/calthoff/tstp/blob/master/part_I/functions/scope_ex1.py</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x = 100</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(&quot;scope 1&quot;)</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(x)</span></p><p class="c1 c9"><span class="c4"></span></p><p class="c1 c9"><span class="c4"></span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def f():</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; y = 200</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; print(&ldquo;scope 2&rdquo;)</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; print(x)</span></p><p class="c1 c9"><span class="c4"></span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;f()</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c4">&rdquo;&rdquo;&rdquo; if you try to use y where this comment is, you will get an </span></p><p class="c0"><span class="c4">error. &rdquo;&rdquo;&rdquo;</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; </span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c4">&gt;&gt; 100</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; scope 1</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; scope 2</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; 200</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">In this example, we have two scopes, which I will call &ldquo;scope 1&rdquo; and &ldquo;scope 2&rdquo; (in programming you don&rsquo;t really name different scopes, this is just to illustrate the point). The first scope is in the &ldquo;global scope;&rdquo; it is made up of every piece of code without an indent. The variables in this scope are called global variables. Every part of our program has access to this scope. Everything inside of our function </span><span class="c15 c3">f()</span><span class="c3">, however, is not in our &ldquo;global scope&rdquo;, its scope is &ldquo;local,&rdquo; and the variables in it are called local variables. That means while</span><span class="c15 c3">&nbsp;f() </span><span class="c3">has access to the global scope (it can use the variable </span><span class="c15 c3">x</span><span class="c3">), the global scope does not have access to </span><span class="c15 c3">f()&rsquo;s</span><span class="c3">&nbsp;local scope, you cannot use variables from </span><span class="c15 c3">f() </span><span class="c3">outside of </span><span class="c15 c3">f()</span><span class="c3">. Modify the example to try to access the variable y from outside of </span><span class="c15 c3">f()</span><span class="c3">&nbsp;and see what happens. Furthermore, while</span><span class="c15 c3">&nbsp;f()</span><span class="c3">&nbsp;has access to the global scope, it cannot change a global variable without special syntax. For example, this will cause an error because we are trying to change a global variable inside of local scope:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c10"><span class="c3"># this code demonstrates an error</span></p><p class="c1 c10"><span class="c3"># https://github.com/calthoff/tstp/blob/master/part_I/functions/scope_ex2.py</span></p><p class="c1 c9 c10"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c4">x = 100</span></p><p class="c1 c9"><span class="c4"></span></p><p class="c1 c9"><span class="c4"></span></p><p class="c1 c10"><span class="c4">def f():</span></p><p class="c1 c10"><span class="c4">&nbsp; &nbsp; x = x + 1</span></p><p class="c1 c10"><span class="c4">&nbsp; &nbsp; print()</span></p><p class="c1 c10"><span class="c4">&nbsp;</span></p><p class="c1 c10"><span class="c4">f()</span></p><p class="c1 c9 c10"><span class="c4"></span></p><p class="c1 c10"><span class="c4">&gt;&gt; ...</span></p><p class="c1 c10"><span class="c4">UnboundLocalError: local variable &#39;x&#39; referenced before assignment</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">To change x, we need to use the </span><span class="c15 c3">global</span><span class="c3">&nbsp;keyword:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# https://github.com/calthoff/tstp/blob/master/part_I/functions/scope_ex3.py</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c4">x = 100</span></p><p class="c1 c9"><span class="c4"></span></p><p class="c1 c9"><span class="c4"></span></p><p class="c1 c10"><span class="c4">def f():</span></p><p class="c1 c10"><span class="c4">&nbsp; &nbsp; global x</span></p><p class="c1 c10"><span class="c4">&nbsp; &nbsp; x += 1</span></p><p class="c1 c10"><span class="c4">&nbsp; &nbsp; print(x)</span></p><p class="c1 c10"><span class="c4">&nbsp;</span></p><p class="c1 c10"><span class="c4">f()</span></p><p class="c1 c9 c10"><span class="c3"></span></p><p class="c1"><span class="c3">Using global variables is not a good practice, and you want to avoid it when you can. The problem with global variables is that when your programs get large, if you have different functions modifying global variables, it can be difficult to keep track of what is happening, and your program quickly becomes unmaintainable. </span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.70d9b42lmrfp"><span>Built-in Functions</span></h3><p class="c1"><span class="c3 c5">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">&ldquo;Standing on the shoulders of giants&rdquo; is important in programming; you want to use as much of other people&rsquo;s work as you can. That means not rewriting code that&rsquo;s already been written. To help accomplish this, Python comes with a whole library of built-in functions ready for you to use called the Python Standard Library. These are functions other people have written, but you have access to. </span></p><p class="c0"><span class="c3">We&rsquo;ve already seen one example of a built-in function, </span><span class="c15 c3">print()</span><span class="c3">, used to print strings to your console. </span><span class="c15 c3">len()</span><span class="c3">&nbsp;is another built in function we can use to return the length of a string:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c0"><span class="c4">len(&ldquo;Monty&rdquo;)</span></p><p class="c1 c9 c10"><span class="c4"></span></p><p class="c1 c10"><span class="c4">&gt;&gt; 5</span></p><p class="c1 c9 c10"><span class="c3"></span></p><p class="c1 c10"><span class="c4">len(&ldquo;Python&rdquo;)</span></p><p class="c1 c10"><span class="c4">&gt;&gt; 6</span></p><p class="c1 c9 c10"><span class="c4"></span></p><p class="c1"><span class="c3">Please note in practice, it is bad form to save a variable for no reason like I did in this example. You should do print(len(&ldquo;Monty&rdquo;)), but for the sake of readability in book format, I created the extra variable. I do this throughout the book, but just remember, in practice, do not store things in variables for no reason. </span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">The built-in function </span><span class="c15 c3">type()</span><span class="c3">&nbsp;returns what type a piece of data is:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c0"><span class="c4">type(&ldquo;Hello World&rdquo;)</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; &lt;type &#39;str&#39;&gt;</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;type(100)</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; &lt;type &#39;int&#39;&gt;</span></p><p class="c1 c9"><span class="c4"></span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;type(1.0)</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; &lt;type &#39;float&#39;&gt;</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">Each type has it&rsquo;s own built-in function as well. </span><span class="c15 c3">str()</span><span class="c3">&nbsp;is used to turn another type into a string, </span><span class="c15 c3">int()</span><span class="c3">&nbsp;to an integer, and </span><span class="c15 c3">float()</span><span class="c3">&nbsp;to a float. </span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c4">str(100)</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; &#39;100&#39;</span></p><p class="c1 c9"><span class="c4"></span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int(1.0)</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; 1</span></p><p class="c1 c9"><span class="c4"></span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;float(100)</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; 100.0</span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.ubswbiqco04h"><span>Built-in Modules</span></h3><p class="c0"><span class="c3">In addition to built-in functions, Python has a wide range of built-in functionality you need to take </span><span class="c3">extra</span><span class="c3">&nbsp;steps in order to use. They are located in something called modules, and you must &ldquo;import&rdquo; these modules from Python in order to use them. The modules built into Python are part of what is called the Python Standard Library. A list of Python&rsquo;s built-in modules for you to explore can be found at </span><span class="c17 c3"><a class="c18" href="https://www.google.com/url?q=https://docs.python.org/2/py-modindex.html&amp;sa=D&amp;ust=1467337426111000&amp;usg=AFQjCNFJoMtIuCf_YuOcW9DjUfX2jdO6Bg">https://docs.python.org/2/py-modindex.html</a></span><span class="c3">. Don&rsquo;t worry about what exactly a module is for now (we cover them in the Modules chapter), just know that you can use them to gain access to functions you wouldn&rsquo;t be able to use otherwise. Here is an example of how you can use Python&rsquo;s built-in math module:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c4">import math</span></p><p class="c1 c9"><span class="c4"></span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;math.factorial(3)</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; 6</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">The basic syntax is to use the </span><span class="c15 c3">import</span><span class="c3">&nbsp;keyword followed by the module you want to use. Generally, you want to do all of the imports for your program at the top of your file. Once you&rsquo;ve imported your module, you can use the syntax </span><span class="c15 c3">module.[function_name]</span><span class="c3">&nbsp;to use a function from the module. In this case the module </span><span class="c15 c3">math</span><span class="c3">&nbsp;has a function called </span><span class="c15 c3">factorial()</span><span class="c3">&nbsp;that we are able to use as if we wrote the function ourselves. You might be wondering how you are supposed to know what functions are available to use? The functions available are all listed in Python&rsquo;s documentation. So if you want to see all the functions available for you to use in Python&rsquo;s math module, you can google &ldquo;Python math module&rdquo; to find the documentation listing all of the functions in the module. </span></p><p class="c0"><span class="c3">The random module is another built-in module that is great for building games. It has a function</span><span class="c15 c3">&nbsp;randint()</span><span class="c3">&nbsp;that lets you pass in two integers and it returns a random number in between them:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# The output of this program might not be 52&mdash;it&rsquo;s random!</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c4">import random</span></p><p class="c1 c9"><span class="c4"></span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;random.randint(0,100)</span></p><p class="c1 c9"><span class="c4"></span></p><p class="c0"><span class="c4">&gt;&gt; 52</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">We can also use the date module to get today&rsquo;s date:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c4">from datetime import datetime</span></p><p class="c1 c9"><span class="c4"></span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(</span><span class="c4">datetime.today()</span><span class="c4">)</span></p><p class="c1 c9"><span class="c4"></span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; &#39;2016-06-15 11:17:15.487153&#39;</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">Don&rsquo;t worry about the new syntax used in this import for now, it&rsquo;s explained in the modules chapter.</span></p><h3 class="c1 c13" id="h.fgjb9wmpraax"><span>Functions vs. Methods</span></h3><p class="c0"><span class="c3">In programming there is a concept similar to functions called methods. For example, </span><span class="c15 c3">datetime.today()</span><span class="c3">&nbsp;is not really a function, it&rsquo;s a method. &nbsp;We &nbsp;learn about methods in </span><span class="c3">P</span><span class="c3">art II, but for the sake of simplicity, I will not be differentiating between functions and methods until after it&rsquo;s explained in Part II. &nbsp;</span></p><p class="c1 c9"><span></span></p><h3 class="c1 c13" id="h.glb19t1qxls9"><span>Wrapping Up</span></h3><p class="c1"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">Now that you understand functions, you can start thinking about an important programming concept&mdash;code reuse. If you program a solution to a problem, you shouldn&rsquo;t code the same solution again if you need it later. Put the solution in a function, and you can </span><span class="c3">use </span><span class="c3">the solution as many times as you want throughout your program. </span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.n5kaxcblq7ei"><span>Challeng</span><span>e</span></h3><p class="c1"><span class="c3">Write a function that does something interesting, and use it more than once in your program. </span></p><h2 class="c1 c13" id="h.rzxed3nea8gn"><span>Chapter </span><span class="c3">5. </span><span>String Manipulation</span></h2><p class="c1"><span class="c3">&ldquo;</span><span class="c3 c79">In theory, there is no difference between theory and practice. But, in practice, there is.</span><span class="c3">&rdquo;</span></p><p class="c1"><span class="c3">&mdash; </span><span class="c3 c79">Jan L. A. van de Snepscheut</span></p><p class="c1 c9"><span></span></p><p class="c1"><span class="c11 c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">Python has some great built</span><span class="c3">-</span><span class="c3">in functionality for manipulating strings, such as changing a string&rsquo;s case or splitting a string into two parts at a given character. This comes in handy anytime you are working with data representing text. Say for example, you load data into your program from a database, but the data IS IN ALL CAPS. Luckily, with Python, this is an easy fix. In this chapter we will go over some of Python&rsquo;s most helpful tools for manipulating strings.</span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.7k9ccz8m2c1g"><span>Change Case</span></h3><p class="c1"><span class="c3">You can change a string so every letter is uppercase by calling upper on it:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c10"><span class="c4">&ldquo;</span><span class="c4 c22">If computer programming were a country, it would be the third most diverse for languages spoken.</span><span class="c4">&rdquo;.upper()</span></p><p class="c1 c9 c10"><span class="c4"></span></p><p class="c1 c10"><span class="c4">&gt;&gt; &ldquo;IF COMPUTER PROGRAMMING WERE A COUNTRY, IT WOULD BE THE THIRD MOST DIVERSE FOR LANGUAGES SPOKEN&rdquo;</span></p><p class="c1"><span class="c41 c3">43</span></p><p class="c1 c9"><span class="c3 c5"></span></p><p class="c1"><span class="c3">Similarly, you can change every letter to lowercase by calling lower on a string:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c10"><span class="c4">&ldquo;Ada Lovelace, the daughter of the English poet Lord Byron, is considered to be the first computer programmer.&rdquo;.lower()</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c10"><span class="c4">&gt;&gt; &ldquo;ada lovelace, the daughter of the english poet lord byron, is &nbsp; &nbsp; &nbsp; &nbsp; considered to be the first computer programmer&rdquo;</span></p><p class="c1"><span class="c41 c3">42</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">You can also capitalize the first letter of every word in a sentence by calling capitalize on a string:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c10"><span class="c4">&rdquo;&rdquo;&rdquo;</span><span class="c4 c33">you can build a computer using anything that can implement a &nbsp; NAND-gate and the concept of zero (i.e., something and nothing). all Turing-complete programming languages are equally powerful (ignoring practicalities). lisp appeared in 1958 and is still regarded as being among the more powerful programming languages today.&rdquo;&rdquo;&rdquo;.capitalize()</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c0"><span class="c4">&gt;&gt; &rdquo;&rdquo;&rdquo;</span><span class="c4 c33">You can build a computer using anything that can implement &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span></p><p class="c0"><span class="c4 c33">a &nbsp;NAND-gate and the &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; concept of zero (i.e., something </span></p><p class="c0"><span class="c4 c33">and nothing). All Turing-complete programming languages are </span></p><p class="c0"><span class="c4 c33">equally powerful (ignoring practicalities). Lisp appeared in 1958 </span></p><p class="c0"><span class="c4 c33">and is still regarded as being among the more powerful </span></p><p class="c0"><span class="c4 c33">programming languages today.&rdquo;&rdquo;&rdquo;</span><span class="c4">&nbsp;</span><span class="c41 c3">40</span></p><p class="c1 c9"><span class="c11 c3"></span></p><h3 class="c1 c13" id="h.d5kpnw9gyhfp"><span>In </span></h3><p class="c1"><span class="c11 c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">The </span><span class="c15 c3">in</span><span class="c3">&nbsp;keyword checks if a substring is in a string:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c10"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c4">&ldquo;Playboy&rdquo; in &nbsp;&rdquo;&rdquo;&rdquo; A picture from Playboy magazine is the </span></p><p class="c0 c10"><span class="c4">most widely used for all sorts </span><span class="c4">of image processing </span></p><p class="c1 c23"><span class="c4">algorithms&rdquo;</span><span class="c4">&rdquo;&rdquo;</span></p><p class="c1 c9"><span class="c4"></span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; True</span></p><p class="c1"><span class="c41 c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;42</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Combine </span><span class="c15 c3">in</span><span class="c3">&nbsp;with </span><span class="c15 c3">not</span><span class="c3">&nbsp;to check if a substring is not in a string:</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p><p class="c0"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&ldquo;</span><span class="c4">Playboy&rdquo; not in </span><span class="c4">&rdquo;&rdquo;&rdquo;</span><span class="c4 c22">The computer virus was initially </span></p><p class="c0 c10"><span class="c4 c22">designed without any harmful intentions</span><span class="c4">&rdquo;&rdquo;&rdquo;</span></p><p class="c1 c9"><span class="c4"></span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; True</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c41 c3">42</span></p><p class="c1 c9"><span class="c11 c3"></span></p><h3 class="c1 c13" id="h.stfy07jf9t7i"><span>Escaping Strings</span></h3><p class="c1"><span class="c3">What if you want to use quotes inside a string? If we insert quotes inside a string we get a syntax error:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c10"><span class="c4">&rdquo;&rdquo;&rdquo;</span><span class="c4">Sun Tzu said &quot;The Supreme art of war is to subdue the enemy without</span><span class="c4">&nbsp;fighting.&quot; &quot; &ldquo;&ldquo;&ldquo;</span></p><p class="c1 c9 c10"><span class="c4"></span></p><p class="c0"><span class="c4">&gt;&gt; SyntaxError: invalid syntax</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p><p class="c1"><span class="c3">We can solve this by </span><span class="c3">doing something called e</span><span class="c3">scaping </span><span class="c3">a</span><span class="c3">&nbsp;character, which means putting a special symbol in front of a character with special meaning in Python (in this case the character is a quote and the special symbol is a </span><span class="c3">back</span><span class="c3">slash), to let Python know that the quote is meant to be a quote in a string, and not a Python keyword representing a new string.</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c4">&ldquo;&ldquo;&ldquo;Sun Tzu said \&quot;The Supreme art of war is to subdue the &nbsp;</span></p><p class="c0 c10"><span class="c4">enemy without fighting.\&quot;</span><span class="c4">&nbsp;</span><span class="c4">&rdquo;&rdquo;&rdquo;</span></p><p class="c0 c9 c10"><span class="c4"></span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; &#39; Sun Tzu said &quot;The Supreme art of war is to subdue the enemy </span></p><p class="c1 c10"><span class="c4">&nbsp; &nbsp;without fighting.&quot; &#39;</span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.pws0uu4mqol6"><span>Index</span></h3><p class="c1"><span class="c35 c3 c5">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">An iterable in Python is something you can iterate through, which means you can go through it one by one. Strings are iterable, which means you can access all of the different characters inside of them using an index, which is a number representing the character&rsquo;s position put inside of brackets after the string. Starting with index 0 and ending with index 2 we can retrieve each individual character from the string &ldquo;LAX&rdquo;. </span></p><p class="c0 c9"><span class="c3"></span></p><p class="c1 c10"><span class="c4">my_string = &ldquo;LAX&rdquo;</span></p><p class="c1 c10"><span class="c4">my_string[0]</span></p><p class="c1 c10"><span class="c4">my_string[1]</span></p><p class="c1 c10"><span class="c4">my_string [2]</span></p><p class="c1 c9 c10"><span class="c4"></span></p><p class="c1 c10"><span class="c4">&gt;&gt; &lsquo;L&rsquo;</span></p><p class="c1 c10"><span class="c4">&gt;&gt; &lsquo;A&rsquo;</span></p><p class="c1 c10"><span class="c4">&gt;&gt; &lsquo;X&rsquo;</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">We used the indexes 0, 1, and 2 to get all of the characters in the string &ldquo;LAX&rdquo;. It is important to remember that indexes start at zero. &nbsp;In Computer Science, we always start counting from zero. So 0 represents the first letter of &ldquo;LAX&rdquo;: &ldquo;L&rdquo;. Counting starting at zero takes some getting used to, so don&rsquo;t worry if it frustrates you at first. If we try to access an element past the last element, we get an error:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c10"><span class="c4">my_string = &ldquo;LAX&rdquo;</span></p><p class="c1 c10"><span class="c4">my_string[3]</span></p><p class="c1 c9 c10"><span class="c4"></span></p><p class="c1 c10"><span class="c4">&gt;&gt; IndexError: string index out of range</span></p><p class="c1 c9"><span class="c11 c3"></span></p><h3 class="c1 c13" id="h.lwi2k2j81mpk"><span>F</span><span>ormat</span></h3><p class="c1"><span class="c35 c3 c5">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">Sometimes you will want to create a string dynamically using variables. We can do this by using the </span><span class="c15 c3">format</span><span class="c3">&nbsp;function on a string:</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c4">year_started = &ldquo;1989&rdquo;</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&ldquo;Python was created in {}.&rdquo;.format(year_started)</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; &lsquo;Python was created in 1989.&rsquo; </span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">The format function looks for any occurrences of &lsquo;{}&rsquo; in the string and replaces them with the values you pass into format. You are not limited to using &lsquo;{}&rsquo; once, you can put as many of them in your string as you&rsquo;d like:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# </span><span class="c17 c3"><a class="c18" href="https://www.google.com/url?q=https://github.com/calthoff/tstp/blob/master/part_I/string_manipulation/format.py&amp;sa=D&amp;ust=1467337426151000&amp;usg=AFQjCNHBICj9-u6RYioyrXhiLo3bJf3a7Q">https://github.com/calthoff/tstp/blob/master/part_I/string_manipulation/format.py</a></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c0"><span class="c4">year_started = &ldquo;1989&rdquo;</span></p><p class="c1 c10"><span class="c4">creator = &ldquo;Guido van Rossum&rdquo; </span></p><p class="c1 c10"><span class="c4">country = &ldquo;the Netherlands&rdquo;</span></p><p class="c1 c10"><span class="c4">my_string = &ldquo;Python was created in {} by {} in {}.&rdquo;.format(year_started, creator, country)</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(my_string)</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; &lsquo;Python was created in 1989 by Guido van Rossum in the &nbsp; &nbsp; &nbsp;</span></p><p class="c0"><span class="c4">Netherlands.&rsquo;</span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.qkv2d8do36kn"><span class="c82">\n</span></h3><p class="c1"><span class="c3 c5">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">Use &nbsp;&ldquo;</span><span class="c3">\n&rdquo;</span><span class="c3">&nbsp;inside a string to represent a newline:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c4">print(</span><span class="c4">&ldquo;line1 \nline2&rdquo;</span><span class="c4">)</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; &lsquo;line1&rsquo;</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt;</span><span class="c4">&nbsp;</span><span class="c4">&lsquo;line2&rsquo;</span></p><p class="c1 c9"><span class="c11 c3"></span></p><h3 class="c1 c13" id="h.8skl39qwbnwc"><span>S</span><span>plit</span></h3><p class="c1"><span class="c11 c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">Strings have a built-in function called split you can use to separate a string into two different strings. You pass the split function the character or characters you want to use to separate the string. For example, we can pass in a period to separate this quote by Daniel Coyle into two strings:</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p><p class="c1 c10"><span class="c4 c22 c56">&rdquo;&rdquo;&rdquo;</span><span class="c4 c56 c22">P</span><span class="c4 c56 c22">ractice doesn&rsquo;t make perfect. Practice makes myelin, and myelin makes perfect.&rdquo;&rdquo;&rdquo;.split(&ldquo;.&rdquo;)</span></p><p class="c1 c9 c10"><span class="c4 c56 c22"></span></p><p class="c1"><span class="c4">&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; [&quot;Practice doesn&#39;t make perfect&quot;, &#39; Practice makes myelin, and </span></p><p class="c0"><span class="c4">myelin makes perfect&#39;, &#39;&#39;]</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">The result is two different strings stored in a list (which we will cover in the next section). </span></p><p class="c1 c9"><span class="c11 c3"></span></p><h3 class="c1 c13" id="h.kg5by1p6m7by"><span>J</span><span>oin</span></h3><p class="c1"><span class="c35 c3 c5">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">The </span><span class="c15 c3">join</span><span class="c3">&nbsp;function lets you add new characters in between every character in a string:</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c4">my_string = &lsquo;abc&rsquo;</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;join_result = &lsquo;+&rsquo;.join(my_string)</span></p><p class="c1 c9"><span class="c4"></span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(join_result)</span></p><p class="c1 c9"><span class="c4"></span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; &lsquo;a+b+c&rsquo;</span></p><p class="c1 c9"><span class="c11 c3"></span></p><h3 class="c1 c13" id="h.c6yi104u3ruk"><span>Wrapping Up</span></h3><p class="c1"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">Python excels at manipulating strings. It is one of the top languages used by </span><span class="c3">d</span><span class="c3">ata </span><span class="c3">s</span><span class="c3">cientists. Manipulating strings is incredibly useful, and with just the knowledge you have so far, you now have the power to easily transform data in a variety of ways. </span></p><p class="c1 c9"><span></span></p><h3 class="c1 c13" id="h.gvz6yazddsxt"><span>Challenge</span></h3><p class="c1"><span>Go to </span><span class="c17"><a class="c18" href="https://www.google.com/url?q=https://www.goodreads.com/work/quotes/7809-the-pragmatic-programmer-from-journeyman-to-master&amp;sa=D&amp;ust=1467337426162000&amp;usg=AFQjCNFhm7F_uw7HQaeWAHgYiDsQlZM77Q">https://www.goodreads.com/work/quotes/7809-the-pragmatic-programmer-from-journeyman-to-master</a></span><span>, pick your favorite quote, copy it into a Python program (make sure to use triple </span><span>quotes </span><span>since the data will be more than one line and Python does not like when you copy and paste quotes) and change the data in as many ways as you can. </span></p><h2 class="c1 c13" id="h.bxron7y2q3ek"><span>Chapter 6. </span><span>Containers</span></h2><p class="c1"><span class="c3">&quot;Bad programmers worry about the code. Good programmers worry about data structures and their relationships.&quot;</span></p><p class="c1"><span class="c3">&mdash; </span><span class="c3 c79 c22">Linus Torvalds</span></p><p class="c1 c9"><span class="c79 c91"></span></p><p class="c1"><span class="c11 c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">So far, the only way we&rsquo;ve learned to store data is in variables. In order to write interesting programs, we need more options. Say for example, you wrote a program to loop over 1000 pieces of data from a database. How could you store that data? Would you save each piece of data in a variable? Would you have to type out 1000 variable names? In programming, we solve this problem by using something called containers, a broad term which also includes data structures which we cover in depth in Part IV. Python comes with many different kinds of built-in containers. Containers are used to store and retrieve data. Containers vary in how they store and retrieve the data, making some of them better suited for certain situations than others. In this chapter, we will go over &nbsp;four commonly used containers: lists, tuples, sets and dictionaries. </span></p><p class="c1 c9"><span class="c11 c3"></span></p><h3 class="c1 c13" id="h.m7y63rpmtmbw"><span class="c35 c5">List</span><span class="c35">s</span></h3><p class="c1"><span class="c3">Python lists are built-in containers that help you store and manage data. </span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p><p class="c1 c10"><span class="c4 c28">my_list = </span><span class="c4 c25">list</span><span class="c4 c28">()</span></p><p class="c1 c10"><span class="c4 c28">my_list.append(</span><span class="c40 c4">&quot;Apple&quot;</span><span class="c4 c28">)</span></p><p class="c1 c10"><span class="c4 c28">my_list.append(</span><span class="c40 c4">&quot;Orange&quot;</span><span class="c4 c28">)</span></p><p class="c1 c10"><span class="c4 c28">my_list.append(</span><span class="c40 c4">&quot;Pear&quot;</span><span class="c4 c28">)</span></p><p class="c1 c10"><span class="c4 c28">my_list</span></p><p class="c1 c9"><span class="c4"></span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; [&#39;Apple&#39;, &#39;Orange&#39;, &#39;Pear&#39;]</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">Once we create a new list with </span><span class="c15 c3">list()</span><span class="c3">, we can &ldquo;add&rdquo; things to the list using its </span><span class="c15 c3">append</span><span class="c3">&nbsp;function, which will place the new item at the front of the list. Lists are not limited to storing strings, they can store any data type:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c4">my</span><span class="c4">_l</span><span class="c4">ist.append(True)</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;my</span><span class="c4">_l</span><span class="c4">ist.append(100)</span></p><p class="c1 c10"><span class="c4">my_list</span></p><p class="c1 c9"><span class="c4"></span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; [&#39;Apple&#39;, &#39;Orange&#39;, &#39;Pear&#39;, True, 100]</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">You can access each item in the list individually by using an index:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c4">my_list[0]</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;my_list[1]</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;my_list[4]</span></p><p class="c1 c9"><span class="c4"></span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; Apple</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; Orange</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; 100</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">Just like a string, every item in a list is at a certain position, called its index. Remember, the &nbsp;first item in a list is not position 1, it is position 0. </span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">You can change an item in the list by setting the index of the item to a new piece of data:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c10"><span class="c4">color_list = [&ldquo;blue&rdquo;, &ldquo;green&rdquo;, &nbsp;&ldquo;yellow&rdquo;]</span></p><p class="c0"><span class="c4">color_list</span></p><p class="c1 c10"><span class="c4">color_list[2] = &ldquo;red&rdquo;</span></p><p class="c1 c10"><span class="c4">color_list</span></p><p class="c1 c9 c10"><span class="c4"></span></p><p class="c1 c10"><span class="c4">&gt;&gt; &nbsp;[&ldquo;blue&rdquo;, &ldquo;green&rdquo;, &nbsp;&ldquo;yellow&rdquo;]</span></p><p class="c1 c10"><span class="c4">&gt;&gt; [&ldquo;blue&rdquo;, &ldquo;green&rdquo;, &ldquo;red&rdquo;]</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">Remove the last item from a list with</span><span class="c15 c3">&nbsp;pop()</span><span class="c3">:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c4">color_list = [&ldquo;blue&rdquo;, &ldquo;green&rdquo;, &nbsp;&ldquo;yellow&rdquo;]</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;color_lis</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;item = </span><span class="c4">color</span><span class="c4">_list.pop()</span></p><p class="c0"><span class="c4">print(item)</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c4">color_list</span></p><p class="c1 c9"><span class="c4"></span></p><p class="c0"><span class="c4">&gt;&gt; </span><span class="c4">[&ldquo;blue&rdquo;, &ldquo;green&rdquo;, &nbsp;&ldquo;yellow&rdquo;]</span><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c4">&gt;&gt; &ldquo;yello</span><span class="c4">w&rdquo;</span></p><p class="c1"><span class="c4">&nbsp; &nbsp; &nbsp; </span><span class="c4">&gt;</span><span class="c4">&gt; </span><span class="c4">&nbsp;</span><span class="c4">[&ldquo;blue&rdquo;, &ldquo;green&rdquo;]</span></p><p class="c0 c9"><span class="c3"></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">Access a range of items with &ldquo;slicing&rdquo;:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c0"><span class="c4">new_list = [&#39;Apple&#39;, &#39;Orange&#39;, &#39;Pear&#39;, True, 100]</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c4">new_list</span><span class="c4">[0:3]</span></p><p class="c1 c9"><span class="c4"></span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; </span><span class="c4">[&#39;Apple&#39;, &#39;Orange&#39;, &#39;Pear&#39;]</span></p><p class="c1 c9"><span class="c4"></span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new_list[3:]</span></p><p class="c1 c9"><span class="c4"></span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; [True, 100]</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">We slice a list by passing in a starting index and an ending index. Our new list (a &ldquo;slice&rdquo; of the old one) is made up of everything between the start and end index. One &ldquo;gotcha&rdquo; is the starting index includes the item at the starting index, whereas the end index doesn&rsquo;t. This means if you want to slice from &ldquo;Apple&rdquo; to &ldquo;Pear&rdquo;, you need to slice from index 0, to index 3, because the slice goes from the start index to the end index -1. </span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">Like a string, you can easily check if something is in a list using the </span><span class="c15 c3">in</span><span class="c3">&nbsp;keyword:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c4">color_list = [&ldquo;blue&rdquo;, &ldquo;green&rdquo;, &nbsp;&ldquo;yellow&rdquo;]</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&ldquo;green&rdquo; in color_list</span></p><p class="c1 c9"><span class="c4"></span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; True</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">You can get the size of a list with the </span><span class="c15 c3">len()</span><span class="c3">&nbsp;function:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c0"><span class="c4">len(color_list)</span></p><p class="c0"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; 3</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">Finally, you can turn a list of strings into a single string by using join:</span></p><p class="c0 c9"><span class="c3"></span></p><p class="c0"><span class="c4">the_</span><span class="c4">F</span><span class="c4">ox = [&ldquo;The&rdquo;, &ldquo;fox&rdquo;, &ldquo;jumped&rdquo;, &ldquo;over&rdquo;, &ldquo;the&rdquo;, &ldquo;fence&rdquo;]</span></p><p class="c0"><span class="c4">one_string = &lsquo;,&rsquo;.join(the_Fox) + &lsquo;.&rsquo;</span></p><p class="c0"><span class="c4">one_string</span></p><p class="c0 c9"><span class="c4"></span></p><p class="c0"><span class="c4">&gt;&gt; The fox jumped over the fence.</span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.mdvpxuvfb30e"><span>Tuples</span></h3><p class="c1"><span class="c35 c3 c5">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">A tuple is a container with many of the same features as a list. The main difference between a list and a tuple is a tuple is immutable. That means unlike a list, once you put data into a tuple, you can no longer change it. You cannot change the value of any of the items in a tuple, &nbsp;you cannot remove items from a tuple, and you cannot add new items to a tuple. If you try, you will get an error:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# this code prints an error</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c4">my_tuple = (&ldquo;brown&rdquo;, &ldquo;orange&rdquo;, &ldquo;yellow&rdquo;)</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;my_tuple[1] = &ldquo;red&rdquo;</span></p><p class="c1 c9"><span class="c4"></span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; Traceback (most recent call last):</span></p><p class="c1 c10"><span class="c4">&nbsp; File &quot;/Users/coryalthoff/PycharmProjects/tstp/more_python/my_tuple.py&quot;, line 3, in &lt;module&gt;</span></p><p class="c1 c10"><span class="c4">&nbsp; &nbsp; my_tuple[0] = &quot;red&quot;</span></p><p class="c1 c10"><span class="c4">TypeError: &#39;tuple&#39; object does not support item assignment</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">You can, however, access data from a tuple like a list&mdash;you can reference an index and slice a tuple:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c4">&nbsp; </span><span class="c4">my_tuple = (&ldquo;brown&rdquo;, &ldquo;orange&rdquo;, &ldquo;yellow&rdquo;)</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c4 c28">my_tuple[</span><span class="c4 c20">0</span><span class="c4 c28">]</span></p><p class="c0"><span class="c4 c28">my_tuple[</span><span class="c4 c20">1</span><span class="c4 c28">:</span><span class="c4 c20">2</span><span class="c4 c28">]</span></p><p class="c1 c9"><span class="c4 c28"></span></p><p class="c1"><span class="c4 c28">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; yellow</span></p><p class="c1"><span class="c4 c28">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; (&#39;yellow&#39;, &#39;orange&#39;)</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">A tuple with one item in it still needs a comma after it:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c4">(&lsquo;self_taught&rsquo;,)</span></p><p class="c1 c9"><span class="c4"></span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; (&lsquo;self_taught&rsquo;,)</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">Make sure to remember this&mdash;I can&rsquo;t count the times this has caused problems for me because I forgot. </span></p><p class="c0"><span class="c3">You may be wondering why you would want to use a data structure that appears to be like a list, but less helpful. Tuples are useful when you are dealing with values you know will never change, and you don&rsquo;t want other parts of your program or someone else to have the ability to change those values. A good example is if you are working with geographic coordinates. You may want to store the longitude and latitude of New York in a tuple because you know the longitude and latitude of New York is never going to change, and you want to make sure other parts of your program don&rsquo;t have the ability to accidentally change them.</span><span class="c11 c3">&nbsp;</span></p><p class="c1 c9"><span class="c11 c3"></span></p><h3 class="c1 c13" id="h.6ahhemnexhhk"><span>Dictionaries</span></h3><p class="c1"><span class="c11 c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">Dictionaries are another built-in container. Unlike lists and tuples, you can use a dictionary to map a key to value.</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p><p class="c0"><span class="c4">my_dictionary = dict()</span></p><p class="c0"><span class="c4">my_dictionary[&ldquo;programming&rdquo;] = &ldquo;awesome&rdquo;</span></p><p class="c0"><span class="c4">my_dictionary[&ldquo;programming&rdquo;]</span></p><p class="c0 c9"><span class="c4"></span></p><p class="c0"><span class="c4">&gt;&gt; awesome &nbsp;</span></p><p class="c0 c9"><span class="c3"></span></p><p class="c0"><span class="c4">my_dictionary[&ldquo;Bill Gates&rdquo;] = &ldquo;rich&rdquo;</span></p><p class="c0"><span class="c4">my_dictionary[&ldquo;Bill Gates&rdquo;]</span></p><p class="c0 c9"><span class="c4"></span></p><p class="c0"><span class="c4">&gt;&gt; rich</span></p><p class="c0 c9"><span class="c3"></span></p><p class="c0"><span class="c4">my_dictionary[&ldquo;america_founded&rdquo;] = 1776</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;my_dictionary[&ldquo;america_founded&rdquo;]</span></p><p class="c1 c9"><span class="c4"></span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; 1776</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">Alternatively, you can define a dictionary like this:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c4">my_dictionary = {&ldquo;programmer&rdquo;: &ldquo;awesome&rdquo;, &ldquo;Bill Gates&rdquo;: &ldquo;rich&rdquo;, </span></p><p class="c0 c95"><span class="c4">&ldquo;</span><span class="c4">A</span><span class="c4">merica_founded</span><span class="c4">&rdquo;</span><span class="c4">: </span><span class="c4">&ldquo;</span><span class="c4">1776</span><span class="c4">&rdquo;</span><span class="c4">}</span></p><p class="c0 c9 c95"><span class="c4"></span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt;</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">Like a list you can use the </span><span class="c15 c3">in</span><span class="c3">&nbsp;keyword to check if a key is in a dictionary:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c4">&ldquo;Bill Gates&rdquo; in my_dictionary</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; True</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c4">&ldquo;Bill Plates&rdquo; in my_dictionary</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; False </span></p><p class="c1 c9"><span class="c3 c11"></span></p><p class="c1"><span class="c3">You also delete a key value pair from a dictionary with the keyword </span><span class="c15 c3">del</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c4">my_dictionary</span><sup><a href="#cmnt8" id="cmnt_ref8">[h]</a></sup></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c7 c4 c5">del </span><span class="c4 c28">my_dict</span><span class="c4 c28">ionary</span><span class="c4 c28">[</span><span class="c40 c4">&#39;Bill Gates&#39;</span><span class="c4 c28">]</span></p><p class="c0"><span class="c4">my_dictionary</span></p><p class="c1 c9"><span class="c4"></span></p><p class="c0"><span class="c4">&gt;&gt; {&#39;america_founded&#39;: 1776, &#39;programming&#39;: &#39;awesome&#39;, &#39;Bill </span></p><p class="c0 c10"><span class="c4">Gates&#39;: &#39;Rich&#39;}</span></p><p class="c0"><span class="c4">&gt;&gt; {&#39;america_founded&#39;: 1776, &#39;programming&#39;: &#39;awesome&#39;}</span></p><h3 class="c1 c13" id="h.e1vw8hbaukhm"><span>Wrapping Up</span></h3><p class="c1"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">Python&rsquo;s containers are incredibly useful, and you will be using them all the time. Using containers is one of the most important parts of programming&mdash;without them, you can&rsquo;t store data in your program, and as you get more advanced, you will want to store data in almost every program you write.</span></p><h3 class="c1 c13" id="h.dughv998w309"><span>Challenge</span></h3><p class="c1"><span class="c3">Lists, tuples and dictionaries are just a few of the containers built-in to Python. Take some time to look up and read about Python sets. What is a situation would you use a set in?</span></p><h2 class="c1 c13" id="h.si7tzj1fsrml"><span>Chapter </span><span>7</span><span>. Loops</span></h2><p class="c1"><span class="c3 c80 c5">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">The second program we wrote printed &ldquo;Hello World&rdquo; a million times. We did this by using something called a loop. Loops let us do things over and over again, and are often used to go through all the data in a container. We can even write a loop that do</span><span class="c3">es</span><span class="c3">&nbsp;something forever&mdash;called an infinite loop. In this chapter we will go over two kinds of loops, for loops and while loops. </span></p><p class="c1 c9"><span class="c11 c3"></span></p><h3 class="c1 c13" id="h.6fat0mgaqsm"><span>For Loops</span></h3><p class="c1"><span class="c11 c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">You can use a for loop to easily execute a set of instructions a certain number of times. This is accomplished with the syntax </span><span class="c15 c3">for i in range(a, z)</span><span class="c3">&nbsp;with &ldquo;a&rdquo; being the number to start at, and &ldquo;z&rdquo; the number to stop at:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# https://github.com/calthoff/tstp/blob/master/part_I/loops/for_loops_ex1.py</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for i in range(0, 10):</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; print(i)</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&gt;&gt; 0</span></p><p class="c1"><span class="c3">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&gt;&gt; 1</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; 2</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; 3</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&hellip;</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">When we use this syntax, </span><span class="c15 c3">i</span><span class="c3">&nbsp;is a variable Python automatically sets to the value of the current number in the range. You can use </span><span class="c15 c3">i</span><span class="c3">&nbsp;just like you would any other variable. The variable does not have to be called </span><span class="c15 c3">i</span><span class="c3">. You can name it whatever you&rsquo;d like. </span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;We can also use a </span><span class="c15 c3">for</span><span class="c3">&nbsp;loop for iteration. Iteration means going one by one through something like a string or list. Something you can iterate is called an iterable. </span></p><p class="c0 c9"><span class="c11 c3"></span></p><p class="c0"><span class="c3"># </span><span class="c17 c3"><a class="c18" href="https://www.google.com/url?q=https://github.com/calthoff/tstp/blob/master/part_I/loops/for_loops_ex2.py&amp;sa=D&amp;ust=1467337426216000&amp;usg=AFQjCNE8PmtwS04PSuExMao9eBr386wuyg">https://github.com/calthoff/tstp/blob/master/part_I/loops/for_loops_ex2.py</a></span></p><p class="c0 c9"><span class="c3"></span></p><p class="c1 c10"><span class="c4">my_string = &ldquo;Python&rdquo;</span></p><p class="c1 c10"><span class="c4">for character in my_string:</span></p><p class="c1 c10"><span class="c4">&nbsp; &nbsp; print character</span></p><p class="c1 c9 c10"><span class="c4"></span></p><p class="c1 c10"><span class="c4">&gt;&gt; &lsquo;P&rsquo;</span></p><p class="c1 c10"><span class="c4">&gt;&gt; &lsquo;y&rsquo;</span></p><p class="c1 c10"><span class="c4">&gt;&gt; &lsquo;t&rsquo;</span></p><p class="c1 c10"><span class="c4">&gt;&gt; &lsquo;h&rsquo;</span></p><p class="c1 c10"><span class="c4">&gt;&gt; &lsquo;o&rsquo;</span></p><p class="c1 c10"><span class="c4">&gt;&gt; &lsquo;n&rsquo; </span></p><p class="c1 c9 c10"><span class="c4"></span></p><p class="c0"><span class="c3"># </span><span class="c17 c3"><a class="c18" href="https://www.google.com/url?q=https://github.com/calthoff/tstp/blob/master/part_I/loops/for_loops_ex3.py&amp;sa=D&amp;ust=1467337426218000&amp;usg=AFQjCNGUm1iLV3Xfq5BlpiAvWJZxAhIjhg">https://github.com/calthoff/tstp/blob/master/part_I/loops/for_loops_ex3.py</a></span></p><p class="c0 c9"><span class="c3"></span></p><p class="c0"><span class="c4">my_list = [&ldquo;a&rdquo;, &ldquo;b&rdquo;, &ldquo;c&rdquo;]</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for character in my_list:</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print character</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; &lsquo;a&rsquo;</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; &lsquo;b&rsquo;</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; &lsquo;c&rsquo;</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">You can use the same syntax to iterate through tuples, sets and dictionaries. However, if you iterate through a dictionary, you will only have access to the dictionaries keys:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3"># </span><span class="c17 c3"><a class="c18" href="https://www.google.com/url?q=https://github.com/calthoff/tstp/blob/master/part_I/loops/for_loops_ex4.py&amp;sa=D&amp;ust=1467337426222000&amp;usg=AFQjCNHHPIbLHVJdYXXa0igH7vrziFb7uw">https://github.com/calthoff/tstp/blob/master/part_I/loops/for_loops_ex4.py</a></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c4">md = {&lsquo;key1&rsquo;: &lsquo;value1&rsquo;, &lsquo;key2&rsquo;: &lsquo;value2&rsquo;}</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for key in md:</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; print(key)</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; &lsquo;key1&rsquo;</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; &lsquo;key2&rsquo;</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">If you want to get both the key and value of a dictionary, you need to use a </span><span class="c3">function</span><span class="c3">&nbsp;called </span><span class="c15 c3">iteritems()</span><span class="c3">, which returns a tuple representing the dictionary&#39;s key value pairs, which you can then iterate through:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c0"><span class="c3"># https://github.com/calthoff/tstp/blob/master/part_I/loops/for_loop_ex5.py</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c0"><span class="c4">md = {&lsquo;key1&rsquo;: &lsquo;value1&rsquo;, &lsquo;key2&rsquo;: &lsquo;value2&rsquo;}</span></p><p class="c0"><span class="c4">print(md)</span></p><p class="c0"><span class="c4">for key, value in md.iteritems():</span></p><p class="c0"><span class="c4">&nbsp; &nbsp; print(key)</span></p><p class="c0"><span class="c4">&nbsp; &nbsp; print(value)</span></p><h3 class="c1 c13" id="h.h6489vu7663k"><span>While Loops</span></h3><p class="c1"><span class="c11 c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">We can use a while loop to do something as long as a certain condition is true. The syntax for creating a while loop is the built-in keyword </span><span class="c15 c3">while</span><span class="c3">, followed by the condition to be evaluated each time the loop runs. The simplest </span><span class="c15 c3">while</span><span class="c3">&nbsp;loop to write is an infinite loop (Prepare to use control-c to do a keyboard interrupt and stop this program from running):</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# </span><span class="c17 c3"><a class="c18" href="https://www.google.com/url?q=https://github.com/calthoff/tstp/blob/master/part_I/loops/while_loops_ex1.py&amp;sa=D&amp;ust=1467337426227000&amp;usg=AFQjCNH1yMOXv2jEIeFvYKKFC12qY51Ldg">https://github.com/calthoff/tstp/blob/master/part_I/loops/while_loops_ex1.py</a></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c4">while True:</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; print(&ldquo;Hello World&rdquo;)</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; Hello World</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">Because this is an infinite loop, without an interrupt, this program will never stop printing &ldquo;Hello World.&rdquo; The reason this is an infinite loop is because the condition we chose is whether </span><span class="c15 c3">True</span><span class="c3">&nbsp;is </span><span class="c15 c3">True</span><span class="c3">&nbsp;and </span><span class="c15 c3">True</span><span class="c3">&nbsp;is always </span><span class="c15 c3">True</span><span class="c3">, therefore this loop will always run. Python enters the loop and says &ldquo;ok </span><span class="c15 c3">True</span><span class="c3">&nbsp;is </span><span class="c15 c3">True</span><span class="c3">&rdquo; so it prints &ldquo;Hello World.&rdquo; Then it evaluates the condition again and once again says &ldquo;ok </span><span class="c15 c3">True</span><span class="c3">&nbsp;is </span><span class="c15 c3">True</span><span class="c3">&rdquo; and prints &ldquo;Hello World</span><span class="c3">.&rdquo;</span><span class="c3">&nbsp;Since </span><span class="c15 c3">True</span><span class="c3">&nbsp;is always going to be </span><span class="c15 c3">True</span><span class="c3">, this happens indefinitely. </span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">Now let&rsquo;s look at a while loop with a condition that will eventually be false:</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# </span><span class="c17 c3"><a class="c18" href="https://www.google.com/url?q=https://github.com/calthoff/tstp/blob/master/part_I/loops/while_loops_ex2.py&amp;sa=D&amp;ust=1467337426232000&amp;usg=AFQjCNHUOK03yTQePqjrebQVnmJJwk_Yvw">https://github.com/calthoff/tstp/blob/master/part_I/loops/while_loops_ex2.py</a></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c0"><span class="c3">x = 10</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;while x &gt; 0:</span></p><p class="c0"><span class="c3">&nbsp; &nbsp; &nbsp;</span><span class="c3">print(&lsquo;{}&rsquo;.format(x))</span></p><p class="c1 c10"><span class="c3">&nbsp; &nbsp; &nbsp; &nbsp; x -= 1</span></p><p class="c1 c10"><span class="c3">&nbsp; &nbsp; &nbsp; &nbsp;print(&ldquo;Happy New Year!&rdquo;)</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c0"><span class="c3">&gt;&gt; 10</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; 9</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; 8</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; 7</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; 6</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; 5</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; 4</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; 3</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; 2</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; 1</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; &lsquo;Happy New Year!&rsquo;</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">The first time through this loop, </span><span class="c15 c3">x</span><span class="c3">&nbsp;will be equal to ten, making </span><span class="c15 c3">x &gt; 0 True</span><span class="c3">. We print the value of </span><span class="c15 c3">x</span><span class="c3">, in this case ten, and decrement </span><span class="c15 c3">x</span><span class="c3">&nbsp;by 1, making x equal to nine. We go through the loop again, this time </span><span class="c15 c3">x</span><span class="c3">&nbsp;is nine and the same thing happens, the value of </span><span class="c15 c3">x</span><span class="c3">&nbsp;is printed and x is decremented by 1, making </span><span class="c15 c3">x</span><span class="c3">&nbsp;now equal to 8. This happens nine more times, until </span><span class="c15 c3">x</span><span class="c3">&nbsp;is finally equal to -1. Once </span><span class="c15 c3">x</span><span class="c3">&nbsp;is equal to -1, </span><span class="c15 c3">x</span><span class="c3">&nbsp;is no longer greater than zero, and our </span><span class="c15 c3">while</span><span class="c3">&nbsp;loop ends, and our program prints &ldquo;Happy New Year!&rdquo;.</span></p><h3 class="c1 c13" id="h.k8jzaxkif0dq"><span>Nested Loops</span></h3><p class="c1"><span class="c11 c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">You can also nest loops inside of each other:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# https://github.com/calthoff/tstp/blob/master/part_I/loops/nested_loops.py</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c4">for i in range(1, 3):</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; print(i)</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; for letter in [&lsquo;a&rsquo;, &lsquo;b&rsquo;, &lsquo;c&rsquo;]:</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; print(letter)</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; 1</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; &lsquo;a&rsquo;</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; &lsquo;b&rsquo;</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; &lsquo;c&rsquo;</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; 2</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; &lsquo;a&rsquo;</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; &lsquo;b&rsquo;</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; &lsquo;c&rsquo;</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;The nested for loop will iterate fully through the list &ldquo;[a&rsquo;, &lsquo;b&rsquo;, &lsquo;c&rsquo;]&rdquo; however many times the outside loop runs&mdash;in this case twice &nbsp;(</span><span class="c4">range(1,3) </span><span class="c3">loops only twice) &nbsp; . </span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">If you have two lists of numbers and want to create a new list with all of the numbers from each list added together you can achieve this with two </span><span class="c15 c3">for</span><span class="c3">&nbsp;loops:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# https://github.com/calthoff/tstp/blob/master/part_I/loops/nested_loops_ex2.py</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c4 c28">list1 = [</span><span class="c4 c20">1</span><span class="c7 c4">, </span><span class="c4 c20">2</span><span class="c7 c4">, </span><span class="c4 c20">3</span><span class="c7 c4">, </span><span class="c4 c20">4</span><span class="c4 c28">]</span></p><p class="c1 c10"><span class="c4 c28">list2 = [</span><span class="c4 c20">5</span><span class="c7 c4">, </span><span class="c4 c20">6</span><span class="c7 c4">, </span><span class="c4 c20">7</span><span class="c7 c4">, </span><span class="c4 c20">8</span><span class="c4 c28">]</span></p><p class="c1 c10"><span class="c4 c28">added_up = []</span></p><p class="c1 c10"><span class="c7 c4 c5">for </span><span class="c4 c28">i </span><span class="c7 c4 c5">in </span><span class="c4 c28">list1:</span></p><p class="c1 c10"><span class="c4 c28">&nbsp; &nbsp;</span><span class="c7 c4 c5">for </span><span class="c4 c28">j </span><span class="c7 c4 c5">in </span><span class="c4 c28">list2:</span></p><p class="c1 c10"><span class="c4 c28">&nbsp; &nbsp; &nbsp; &nbsp;added_up.append(i + j)</span></p><p class="c1 c9 c10"><span class="c4 c28"></span></p><p class="c1 c10"><span class="c7 c4 c5">print</span><span class="c4 c28">(added</span><span class="c4 c28">_up)</span></p><p class="c1 c9"><span class="c4"></span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; [6, 7, 8, 9, 7, 8, 9, 10, 8, 9, 10, 11, 9, 10, 11, 12]</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">You can also nest a </span><span class="c15 c3">for</span><span class="c3">&nbsp;loop inside a </span><span class="c15 c3">while</span><span class="c3">&nbsp;loop and vice versa:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# https://github.com/calthoff/tstp/blob/master/part_I/loops/nested_loops_ex3.py</span></p><p class="c1 c10"><span class="c7 c4 c5">while </span><span class="c4 c25">input</span><span class="c4 c28">(</span><span class="c40 c4">&#39;Continue y or n?&#39;</span><span class="c4 c28">) != </span><span class="c40 c4">&#39;n&#39;</span><span class="c4 c28">:</span></p><p class="c1 c10"><span class="c4 c28">&nbsp; &nbsp;</span><span class="c7 c4 c5">for </span><span class="c4 c28">i </span><span class="c7 c4 c5">in </span><span class="c4 c25">range</span><span class="c4 c28">(</span><span class="c4 c20">5</span><span class="c4 c28">):</span></p><p class="c1 c10"><span class="c4 c28">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c7 c4 c5">print</span><span class="c4 c28">(i)</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">This program will indefinitely print the numbers 0-4 until the user enters &ldquo;n&rdquo;. </span><span class="c3">In Python and most programing languages, &ldquo;!&rdquo; means &ldquo;not.&rdquo;</span><sup><a href="#cmnt9" id="cmnt_ref9">[i]</a></sup><span class="c3">&nbsp;</span><span class="c3">Try switching this program to make the </span><span class="c15 c3">while</span><span class="c3">&nbsp;loop nested inside the </span><span class="c15 c3">for</span><span class="c3">&nbsp;loop and see what happens</span><span class="c3">.</span></p><p class="c1"><span class="c3">&gt;&gt;</span></p><p class="c1"><span class="c3">Continue y or n?y</span></p><p class="c1"><span class="c3">0</span></p><p class="c1"><span class="c3">1</span></p><p class="c1"><span class="c3">2</span></p><p class="c1"><span class="c3">3</span></p><p class="c1"><span class="c3">4</span></p><p class="c1"><span class="c3">Continue y or n?y</span></p><p class="c1"><span class="c3">0</span></p><p class="c1"><span class="c3">1</span></p><p class="c1"><span class="c3">2</span></p><p class="c1"><span class="c3">3</span></p><p class="c1"><span class="c3">4</span></p><p class="c1"><span class="c3">Continue y or n?n</span></p><p class="c1"><span class="c3">&gt;&gt;&gt;</span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.o05h5unmo6qs"><span>Break</span></h3><p class="c1"><span class="c35 c3 c5">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">You can stop a loop from executing by using the keyword </span><span class="c15 c3">break</span><span class="c3">. For instance, the following loop would run 100 times:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# </span><span class="c17 c3"><a class="c18" href="https://www.google.com/url?q=https://github.com/calthoff/tstp/blob/master/part_I/loops/break.py&amp;sa=D&amp;ust=1467337426260000&amp;usg=AFQjCNEjqpIXNDj6R32lkrq19VaXt01h4g">https://github.com/calthoff/tstp/blob/master/part_I/loops/break.py</a></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c0"><span class="c4">for i in range(0, 100):</span></p><p class="c0"><span class="c4">&nbsp; &nbsp; print(i)</span></p><p class="c0 c9"><span class="c4"></span></p><p class="c0"><span class="c4">&gt;&gt; 0</span></p><p class="c0"><span class="c4">&gt;&gt; 1</span></p><p class="c0"><span class="c4">...</span></p><p class="c0 c9"><span class="c3"></span></p><p class="c1"><span class="c3">But if we add a </span><span class="c15 c3">break</span><span class="c3">&nbsp;statement:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# </span><span class="c17 c3"><a class="c18" href="https://www.google.com/url?q=https://github.com/calthoff/tstp/blob/master/part_I/loops/break_ex2.py&amp;sa=D&amp;ust=1467337426263000&amp;usg=AFQjCNFKQ86ziEPifCMZ334dXJwhDraYtw">https://github.com/calthoff/tstp/blob/master/part_I/loops/break_ex2.py</a></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c0"><span class="c4">for i in range(0, 100):</span></p><p class="c0"><span class="c4">&nbsp; &nbsp; print(i)</span></p><p class="c0"><span class="c4">&nbsp; &nbsp; break</span></p><p class="c0 c9"><span class="c4"></span></p><p class="c0"><span class="c4">&gt;&gt; 0</span></p><p class="c0 c9"><span class="c3"></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">T</span><span class="c3">he loop goes around once, prints the number zero, hits the </span><span class="c15 c3">break</span><span class="c3">&nbsp;keyword, and stops. This is useful in all kinds of situations. For example, we can now write a program that keeps asking the user for input until they type &ldquo;q&rdquo; to quit:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# </span><span class="c17 c3"><a class="c18" href="https://www.google.com/url?q=https://github.com/calthoff/tstp/blob/master/part_I/loops/break_ex3.py&amp;sa=D&amp;ust=1467337426267000&amp;usg=AFQjCNEOF4dfAvAFAwZc2nRHxjR3N5Lvcg">https://github.com/calthoff/tstp/blob/master/part_I/loops/break_ex3.py</a></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&ldquo;&ldquo;&ldquo;</span><span class="c3">If you are unfamiliar the reference in this example, go watch Monty Python and the Holy </span></p><p class="c0"><span class="c3">Grail!&rdquo;&rdquo;&rdquo;</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c10"><span class="c4">questions = [&ldquo;What is your name?&rdquo;, &ldquo;What is your favorite color?&rdquo;, &ldquo;What is your quest?&rdquo;]</span></p><p class="c1 c10"><span class="c4">n = 0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;while True:</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; print(&ldquo;Type q to quit&rdquo;)</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; answer = input(questions[n])</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; if answer == &lsquo;q&rsquo;:</span></p><p class="c1 c10"><span class="c4">&nbsp; &nbsp; &nbsp; &nbsp; break</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; n += 1</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; if n &gt; 2:</span></p><p class="c0"><span class="c4">&nbsp; &nbsp; &nbsp; &nbsp; n = 0</span></p><p class="c0 c9"><span class="c3"></span></p><p class="c1"><span class="c3">Each time through our infinite loop, our program will ask the user a question from our list of questions. This will go on indefinitely, unless the user types in &ldquo;q&rdquo; , in which &nbsp;case we hit the </span><span class="c15 c3">break</span><span class="c3">&nbsp;keyword and the loop terminates, ending our program.</span></p><p class="c0 c9"><span class="c11 c3"></span></p><h3 class="c1 c13" id="h.57m96sr3sr8e"><span>Continue</span></h3><p class="c1"><span class="c35 c3 c5">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">You can use the keyword </span><span class="c15 c3">continue</span><span class="c3">&nbsp;to jump back to the top of a loop. Say for instance, we want to print the numbers from 1 to 5, except for the number 3. We can do this by using a </span><span class="c15 c3">for</span><span class="c3">&nbsp;loop with the </span><span class="c15 c3">continue</span><span class="c3">&nbsp;keyword:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# </span><span class="c17 c3"><a class="c18" href="https://www.google.com/url?q=https://github.com/calthoff/tstp/blob/master/part_I/loops/continue.py&amp;sa=D&amp;ust=1467337426274000&amp;usg=AFQjCNENS6uhjuNS3yetnNy5Ubr2_EZqlw">https://github.com/calthoff/tstp/blob/master/part_I/loops/continue.py</a></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c4">for i in </span><span class="c4">range(1, 6)</span><sup><a href="#cmnt10" id="cmnt_ref10">[j]</a></sup><span class="c4">:</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; if i == 3:</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; continue</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; print(i)</span></p><p class="c1 c9"><span class="c4"></span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; 1</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; 2</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; 4</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; 5</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">In our loop, when </span><span class="c15 c3">i</span><span class="c3">&nbsp;becomes equal to 3, we hit our </span><span class="c15 c3">continue</span><span class="c3">&nbsp;keyword. Instead of causing our loop to exit completely like the </span><span class="c15 c3">break</span><span class="c3">&nbsp;keyword, </span><span class="c15 c3">continue</span><span class="c3">&nbsp;keeps the loop going but simply jumps to the top of the loop. The result is that when </span><span class="c15 c3">i</span><span class="c3">&nbsp;is equal to 3, our loop jumps to the top, skipping the print statement when </span><span class="c15 c3">i</span><span class="c3">&nbsp;is equal to 3 and only printing 1, 2, 4 and 5. &nbsp;</span></p><h3 class="c1 c13" id="h.z8axj6c07xr"><span>Wrapping Up</span></h3><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;With the addition of loops in your arsenal, you can now write powerful programs that do the same thing over and over until you tell them to stop. </span></p><p class="c1 c9"><span></span></p><h3 class="c1 c13" id="h.9v0vvm1gaug0"><span>Challenge</span></h3><p class="c1"><span class="c3">Write a program that runs an infinite loop (with q to quit), and each time through the loop, it asks the user to guess a number and tells them whether their guess was right or wrong. </span></p><h2 class="c1 c13" id="h.e20r9o8e78ck"><span>Chapter 8. Modules</span></h2><p class="c1"><span class="c11 c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">Imagine if we created a large project with ten thousand lines of code. If we kept all of that code in one file, it would be difficult to understand. Every time something broke, we would have to scroll through a file with ten thousand lines. Programmers solve this problem by dividing large programs up into multiple pieces, with each piece stored in its own module. A module is simply a </span><span class="c3">P</span><span class="c3">ython file inside a Python Package. A Python package is just a folder with an empty file in it named &ldquo;__init__.py&rdquo; (two underscores</span><span class="c3">, then the word &ldquo;init&rdquo; then two more underscores</span><span class="c3">).</span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.gu80v3nwzts"><span>Creating a Module</span></h3><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;In this section, we are going to create a module, and use code from it in another Python program. First create a new folder on your computer called &ldquo;tstp&rdquo;. Inside this new folder, create an empty Python file called &ldquo;__init__.py&rdquo;. Make sure to use two underscores</span><span class="c3">&nbsp;before and after &ldquo;init.&rdquo;</span><span class="c3">&nbsp;Create another file called &ldquo;hello.py&rdquo;.</span><span class="c3">&nbsp;</span><span class="c3">(When you save a file from IDLE, IDlE automatically adds the .py.)</span><sup><a href="#cmnt11" id="cmnt_ref11">[k]</a></sup><span class="c3">&nbsp;</span><span class="c3">Inside &ldquo;hello.py&rdquo; write a function to print &ldquo;hello&rdquo;.</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# </span><span class="c17 c3"><a class="c18" href="https://www.google.com/url?q=https://github.com/calthoff/tstp/blob/master/part_I/modules/hello.py&amp;sa=D&amp;ust=1467337426283000&amp;usg=AFQjCNGYd4mybtoG0RFzUzBll4FdzI-BLQ">https://github.com/calthoff/tstp/blob/master/part_I/modules/hello.py</a></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c4">def print_hello():</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; print(&ldquo;Hello&rdquo;)</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">Save the file. </span><span class="c3">That&rsquo;s all we have to do. We now have a Python module we can use in other programs. </span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">Inside the &ldquo;tstp&rdquo; folder, create a new Python file called &ldquo;project.py&rdquo;. Inside &ldquo;project.py&rdquo; add the following code:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# </span><span class="c17 c3"><a class="c18" href="https://www.google.com/url?q=https://github.com/calthoff/tstp/blob/master/part_I/modules/project.py&amp;sa=D&amp;ust=1467337426286000&amp;usg=AFQjCNHFa1lgMiQ99dhS2aRx48kX_p8SDg">https://github.com/calthoff/tstp/blob/master/part_I/modules/project.py</a></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c4">from hello import print_hello</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print_hello()</span></p><p class="c1 c9"><span class="c4"></span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; &lsquo;Hello&rsquo;</span></p><p class="c1 c9"><span class="c11 c3"></span></p><p class="c1"><span class="c3">The line </span><span class="c15 c3">from hello import print_hello</span><span class="c3">&nbsp;imported the function </span><span class="c15 c3">print_hello()</span><span class="c3">&nbsp;into our program. Once it&rsquo;s imported, we have full access to it and can can use it in our program whenever we&rsquo;d like, in this case calling the function once with </span><span class="c15 c3">print_hello()</span><span class="c3">. </span></p><p class="c0"><span class="c3">The syntax for importing a module is to use the keyword </span><span class="c15 c3">from</span><span class="c3">,</span><span class="c3">&nbsp;followed by the name of the module we are importing code from, followed by the name of what we want to import (in this case the function &ldquo;print_hello&rdquo;). </span></p><p class="c1 c9"><span class="c11 c3"></span></p><h3 class="c1 c13" id="h.9ahcnyksia9u"><span>Dot Notation</span></h3><p class="c1"><span class="c11 c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">You can also import modules. using dot notation </span><span class="c3">This is called as a relative import</span><sup><a href="#cmnt12" id="cmnt_ref12">[l]</a></sup><span class="c3">. </span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c4">from package1.package2.package3.my_module import my_function</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">In this example, imagine there is a Python file saved on your computer at package1/package2/package3/my_module.py (each package is a python module). We are telling Python to start in the module package1 and look for package2, look in package2 for package3 and to look in package 3 for a file called &ldquo;my_module.py&rdquo;. Finally, inside &ldquo;my_module.py&rdquo; Python should import whatever has the name &ldquo;my_function&rdquo;, whether that is a function, a variable or something else. &nbsp;</span></p><p class="c1 c9"><span class="c11 c3"></span></p><h3 class="c1 c13" id="h.fokw2739tw64"><span>Troubleshooting Imports</span></h3><p class="c1"><span class="c11 c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">If you are trying to import something from a module and it&rsquo;s not working, the first thing to do is make sure you </span><span class="c3">have </span><span class="c3">create</span><span class="c3">d</span><span class="c3">&nbsp;an &ldquo;__init__.py&rdquo; file. If you are still having problems, the next thing to do is to look at the system path. The system path is a list of directories </span><span class="c3">you are allowed import from</span><span class="c3">. You can view this by using the sys module:</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;import sys</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sys.path</span></p><p class="c1 c9"><span class="c4"></span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; </span></p><p class="c0"><span class="c4">[&#39;/Users/coryalthoff/.virtualenvs/tstp/lib/python2.7/site-package</span></p><p class="c0"><span class="c4">s&#39;, &#39;/Users/coryalthoff/PycharmProjects/tstp&#39;]</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">In this example, there are are two directories in the system path, &lsquo;site-packages&rsquo; and &lsquo;tstp&rsquo;. &ldquo;site-packages&rdquo; is a folder automatically created when you install Python, and &ldquo;tstp&rdquo; is a Python package I created. That means if we try to import a module from either of these locations, Python will be able to find it. If you are trying to start an import at a location not in your system path, it will not work. You need to start at one of these locations. </span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.lp6hdlau3olo"><span>Wrapping Up</span></h3><p class="c1"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">Modules allow you to split your code up, making it easier to work with (programmers use the word maintainable). If you find yourself frustrated by how much code is in a file, try splitting it into multiple modules. </span></p><h3 class="c1 c13" id="h.s3bllsvku6cn"><span>Challenge</span></h3><p class="c0"><span class="c3">I challenge you to write three functions in a module, and use them in another Python program. </span></p><h2 class="c1 c13" id="h.8x19bmyu93qt"><span>Chapter</span><span class="c11">&nbsp;</span><span>9. Files</span></h2><p class="c1"><span class="c11 c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">Python makes it easy to work with files. As you can imagine, being able to manipulate the data in files is incredibly useful. In this chapter, we will learn the basics of working with files.</span></p><h3 class="c1 c13" id="h.5nqsmwtzcwkc"><span>Working With Files</span></h3><p class="c1"><span class="c3">T</span><span class="c3">he</span><span class="c3">&nbsp;</span><span class="c15 c3">open</span><span class="c3">() function </span><span class="c3">allows us to open a file in Python so we can read data from it and write data to it. The </span><span class="c15 c3">open() </span><span class="c3">function</span><span class="c3">&nbsp;can create a new file if the file doesn&rsquo;t already exist (depending on the second parameter you pass in):</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c4">my_file = open(&ldquo;my_file.txt&rdquo;, &ldquo;w&rdquo;)</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;my_file.write(&ldquo;Hello from Python!&rdquo;)</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;my_file.close()</span></p><p class="c1 c9"><span class="c4"></span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt;</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">When you run this code, </span><span class="c4">open()</span><span class="c3">&nbsp;creates a new file called &ldquo;my_file.txt&rdquo; in whatever directory you ran your program in. </span><span class="c4">open()</span><span class="c3">&nbsp;creates a file object we can use to work with our new file, which we save in the variable &ldquo;my_file&rdquo;. We can use this variable to write to the file using the </span><span class="c4">write()</span><span class="c3">&nbsp;function which accepts a string as a parameter and writes it to the file. The second parameters of </span><span class="c4">open()</span><span class="c3">&nbsp;determines the &ldquo;mode&rdquo; the file will be opened in. In this case we passed in &ldquo;w&rdquo;, which opens the file in &ldquo;writing&rdquo; mode. There are several other modes you can use as well. Here are some of the most popular:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c4">&ldquo;r&rdquo; </span><span class="c3">&nbsp;</span><span class="c3 c16 c22">Opens a file for reading only. The file pointer is placed at the beginning of the file. This is the default mode.</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c4">&ldquo;w&rdquo;</span><span class="c3">&nbsp; </span><span class="c3 c16 c22">Opens a file for writing only. Overwrites the file if the file exists. If the file does not exist, creates a new file for writing.</span></p><p class="c1 c9"><span class="c3 c16 c22"></span></p><p class="c1"><span class="c3 c16 c22">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c4 c16 c22">&ldquo;w+&rdquo;</span><span class="c3 c16 c22">&nbsp;Opens a file for both writing and reading. Overwrites the existing file if the file exists. If the file does not exist, creates a new file for reading and writing.</span></p><p class="c1 c9"><span class="c3 c16 c22 c77"></span></p><p class="c1"><span class="c41 c77 c3 c22">5</span></p><p class="c1 c9"><span class="c11 c3"></span></p><p class="c1"><span class="c3">Finally, </span><span class="c3">in the example above, </span><span class="c3">we close</span><span class="c3">d</span><span class="c3">&nbsp;the file by calling the </span><span class="c4">close()</span><span class="c3">&nbsp;method on the file object. </span></p><h3 class="c1 c13" id="h.8kgras3xizrh"><span>Using with</span></h3><p class="c1"><span class="c35 c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">While the syntax we just covered can be used to open a file, the preferred way to open a file in Python is to use the syntax </span><span class="c4">with open(&lsquo;my_file&rsquo;,[mode]) as [variable_name]</span><span class="c3">:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# https://github.com/calthoff/tstp/blob/master/part_I/files/using_with.py</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c4">with open(&lsquo;my_file.txt&rsquo;, &lsquo;w&rsquo;) as my_file:</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;my_file.write(&lsquo;Hello from Python!&rsquo;)</span></p><p class="c1 c9"><span class="c4"></span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt;</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">With this syntax we don&rsquo;t have to remember to call </span><span class="c4">close()</span><span class="c3">&nbsp;on &nbsp;our file object, which causes problems if we forget to do it. As long as you are inside the scope of the </span><span class="c4">with</span><span class="c3">&nbsp;statement, you can work with the file you opened, using whatever variable you named your file-like object, in this case &ldquo;my_file&rdquo;. As soon as Python leaves the scope of the </span><span class="c4">with</span><span class="c3">&nbsp;statement, it automatically closes the file for you</span><span class="c11 c3">. </span></p><p class="c1 c9"><span class="c11 c3"></span></p><h3 class="c1 c13" id="h.aa8bphpnsja2"><span>Reading Files</span></h3><p class="c1"><span class="c11 c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">If you want to read a file, open it for reading by passing in </span><span class="c4">&ldquo;r&rdquo;</span><span class="c3">&nbsp;as the second parameter:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# </span><span class="c17 c3"><a class="c18" href="https://www.google.com/url?q=https://github.com/calthoff/tstp/blob/master/part_I/files/reading_files.py&amp;sa=D&amp;ust=1467337426313000&amp;usg=AFQjCNFhGNrbyLZ1FmByPw4LfPl9pM21PA">https://github.com/calthoff/tstp/blob/master/part_I/files/reading_files.py</a></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c4">with open(&ldquo;my_file.txt&rdquo;, &ldquo;r&rdquo;) as my_fi</span><span class="c4">l</span><span class="c4">e:</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for line in my_file.read():</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; print(line)</span></p><p class="c1 c9"><span class="c4"></span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;</span><span class="c4">&gt; Hello from Python!</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">You can call </span><span class="c4">read()</span><span class="c3">&nbsp;on the file object to iterate through each line of the file. You can only call </span><span class="c4">read()</span><span class="c3">&nbsp;on a file once, so you will want to save the result in a variable if you need to use the file contents again later</span><span class="c3">&nbsp;in the same program</span><span class="c3">. </span></p><p class="c1 c9"><span class="c11 c3"></span></p><h3 class="c1 c13" id="h.nv20yhyqhjmf"><span>CSV Files</span></h3><p class="c1"><span class="c11 c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">Python comes with a built-in CSV module for working with CSV files. CSV stands for comma separated value and is a file format where values are separated by commas. A comma in a comma</span><span class="c3">-</span><span class="c3">separated file is called a delimiter. It appears in the file, but it is only used to separate things. Here is an example of the contents of a csv file:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c4"># my_file.csv</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;one, two, three</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">You could load this file into excel, and &ldquo;one&rdquo;, &ldquo;two&rdquo; and &ldquo;three&rdquo; would each get their own column. </span><span class="c3">Excel uses the commas to determine where each row ends (the commas are not displayed to the user) and each new line in the file is a new column. We can create a file called &ldquo;my_file.csv&rdquo; and write &ldquo;one, two, three&rdquo; to it with the csv module:</span><sup><a href="#cmnt13" id="cmnt_ref13">[m]</a></sup></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# </span><span class="c17 c3"><a class="c18" href="https://www.google.com/url?q=https://github.com/calthoff/tstp/blob/master/part_I/files/csv_files.py&amp;sa=D&amp;ust=1467337426320000&amp;usg=AFQjCNERhVFV-c_sHw_QyVCnzonCr6J1-Q">https://github.com/calthoff/tstp/blob/master/part_I/files/csv_files.py</a></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c4 c5 c42">import</span><span class="c4 c33">&nbsp;</span><span class="c4 c5 c84">csv</span></p><p class="c1"><span class="c4 c33"><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c4 c42 c5">with</span><span class="c4 c33">&nbsp;</span><span class="c4 c42">open</span><span class="c4 c33">(&lsquo;</span><span class="c4 c24">my_file.csv&rsquo;</span><span class="c4 c33">, </span><span class="c4 c24">&lsquo;w&rsquo;</span><span class="c4 c33">) </span><span class="c4 c42 c5">as</span><span class="c4 c33">&nbsp;csvfile:<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp;spamwriter </span><span class="c4 c50">=</span><span class="c4 c33">&nbsp;csv</span><span class="c4 c50">.</span><span class="c4 c33">writer(csvfile, delimiter</span><span class="c4 c50">=</span><span class="c4 c24">&#39;,&rsquo;)</span><span class="c4 c33"><br> &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span class="c4 c33">&nbsp; spamwriter</span><span class="c4 c50">.</span><span class="c4 c33">writerow([</span><span class="c4 c24">&lsquo;one&rsquo;, &lsquo;two&rsquo;, &lsquo;three&rsquo;</span><span class="c4 c33">])</span></p><p class="c1 c9"><span class="c3 c33"></span></p><p class="c0"><span class="c3 c33">&gt;&gt;<br> &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p><p class="c1"><span class="c3">We use the </span><span class="c4">with</span><span class="c3">&nbsp;keyword to open our file just like the previous example. However, this time, we call the</span><span class="c4">&nbsp;csv.writer()</span><span class="c3">&nbsp;method from the csv module and pass in our file name as a parameter, as well as &lsquo;,&rsquo;. The second parameter is what we want our delimiter to be, in this case we want it to be a comma. Now we can call </span><span class="c4">csv.</span><span class="c4">writerow()</span><span class="c15 c3">&nbsp;</span><span class="c3">on our csv object, and pass it a list of strings we want to write to our file. Every item in the list is in the same row (if you want to write another row, simply call </span><span class="c4">writerow() </span><span class="c3">again with another list). When you run this program, it will create a new file called &ldquo;my_file.csv&rdquo; and when you open the file with a text editor, it will look like this:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c4"># my_file.csv</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;on</span><span class="c4">e, two, th</span><span class="c4">ree</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">We can also use the csv module to &nbsp;read the contents of a file:</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# </span><span class="c17 c3"><a class="c18" href="https://www.google.com/url?q=https://github.com/calthoff/tstp/blob/master/part_I/files/csv_files_ex2.py&amp;sa=D&amp;ust=1467337426327000&amp;usg=AFQjCNF5FzZ4_c0VcHEmZaWbs0tjki60Dg">https://github.com/calthoff/tstp/blob/master/part_I/files/csv_files_ex2.py</a></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c4">import csv</span></p><p class="c1 c9"><span class="c4"></span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;with open(&lsquo;my_file.csv&rsquo;, &lsquo;r&rsquo;) as csvfile:</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c4 c33">spamreader </span><span class="c4 c50">=</span><span class="c4 c33">&nbsp;csv</span><span class="c4 c50">.</span><span class="c4 c33">reader(csvfile, delimiter</span><span class="c4 c50">=</span><span class="c4 c24">&#39;,&#39;</span><span class="c4 c33">)<br></span><span class="c4 c44 c5">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; </span><span class="c4 c42 c5">for</span><span class="c4 c33">&nbsp;row </span><span class="c4 c42 c5">in</span><span class="c4 c33">&nbsp;spamreader:<br></span><span class="c4 c44 c5">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; </span><span class="c4 c42 c5">print</span><span class="c4 c33">(</span><span class="c4 c24">&#39;,&#39;</span><span class="c4 c50">.</span><span class="c4 c33">join(row))</span></p><p class="c1 c9"><span class="c4 c33"></span></p><p class="c1"><span class="c4 c33">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt;</span><span class="c4 c33">&nbsp;one,two,thre</span><span class="c4 c33">e</span></p><p class="c1 c9"><span class="c3 c33"></span></p><p class="c1"><span class="c3 c33">This time we pass in &ldquo;r&rdquo; to open as as the second parameter. This opens the file for reading only. Instead of calling </span><span class="c15 c3 c33">csv.writer()</span><span class="c3 c33">&nbsp;like the previous example, we call </span><span class="c15 c3 c33">csv.reader() </span><span class="c3 c33">but still pass in the name of the file and a comma as the delineator. Now we can iterate through </span><span class="c15 c3 c33">spamreader</span><span class="c3 c33">, and print each row. </span></p><p class="c1 c9"><span class="c11 c3 c33"></span></p><h3 class="c1 c13" id="h.9s2c2stcczxf"><span>Wrapping Up</span></h3><p class="c1"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">Up until now our programs have not been able to </span><span class="c15 c3">persist </span><span class="c3">data (</span><span class="c3">make </span><span class="c3">data outl</span><span class="c3">ive</span><span class="c3">&nbsp;the program that collected it). Once our program shuts down, any information we collected from the user is lost. Files are one way to persist data. When I was working at eBay, all of the data in our data pipeline started as text files. </span></p><p class="c1 c9"><span></span></p><h3 class="c1 c13" id="h.sezzad5njwxc"><span>Challenge</span></h3><p class="c1"><span class="c3">Write a program that collects data from a user, and saves it to a file, so the data persists.</span></p><p class="c1 c9"><span></span></p><h2 class="c1 c13" id="h.cgcuqoku04uo"><span>Chapter 10. Let&rsquo;s Read Some Code</span><sup><a href="#cmnt14" id="cmnt_ref14">[n]</a></sup></h2><p class="c0"><span class="c3">One of the best ways at getting better at programming is by reading other people&rsquo;s code. I want you to be exposed to as many different ways to use the concepts we&rsquo;ve gone over as possible, so this chapter is going to be a little different&mdash;I am simply going to present examples of code with no explanations, and I want you to read them, copy them and try to understand how they work. &nbsp;</span></p><h2 class="c1 c13" id="h.8p4qs5ghjdcy"><span>Chapter</span><span class="c11 c3">&nbsp;</span><span>11. </span><span>Bringing It All Together</span></h2><p class="c0"><span class="c3">So what can we do with what we&rsquo;ve learned so far? It turns out, we can do quite a bit. In fact, we can write every program in the world! </span><span class="c3">With what you&rsquo;ve learned so far, you can write every program in the world</span><sup><a href="#cmnt15" id="cmnt_ref15">[o]</a></sup><span class="c3">&nbsp;(although not necessarily efficiently). Nevertheless, it is an awesome achievement. In this chapter, we will build a game using what we&rsquo;ve learned so far. Some programming books dive right into building games with graphical user interfaces, however, adding a GUI so early is a distraction, so in this chapter we will build a text-based game. </span></p><p class="c0 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.z70y9ptatqku"><span class="c35 c5">Hangman</span><span class="c11">&nbsp;</span></h3><p class="c1"><span class="c11 c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">We will start by building the classic game Hangman. If you&rsquo;ve never played Hangman, here&#39;s how it works. Player One picks a secret word and draws lines i.e., &ldquo;__&rdquo; &nbsp;representing each letter in the word. Player Two tries to guess the word one letter at a time. If Player Two guess a letter correctly, the corresponding underscore is turned to a letter. If Player Two guesses incorrectly</span><span class="c3">,</span><span class="c3">&nbsp;Player One draws a piece of a picture of a hangman. If Player Two completes the word before the picture of the hangman is drawn, he wins, if not he loses. Are you ready to build Hangman? Here is the first part of our game:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# https://github.com/calthoff/tstp/blob/master/part_I/bringing_it_all_together/hangman.py</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c0"><span class="c7 c3 c5">def </span><span class="c2 c5">hangman</span><span class="c2">():</span></p><p class="c1 c23"><span class="c2">&nbsp; &nbsp;</span><span class="c2">word = </span><span class="c40 c3">&quot;cat&quot;</span><sup><a href="#cmnt16" id="cmnt_ref16">[p]</a></sup></p><p class="c1 c23"><span class="c2">&nbsp; &nbsp;stage = </span><span class="c3 c20">0</span></p><p class="c0 c10"><span class="c3 c20">&nbsp; &nbsp;</span><span class="c2">stages = [</span><span class="c40 c3">&quot;&quot;</span><span class="c7 c3">, </span><span class="c40 c3">&quot;________ &nbsp; &nbsp; &nbsp;&quot;</span><span class="c7 c3">, </span><span class="c40 c3">&quot;| &nbsp; &nbsp; &nbsp;| &nbsp; &nbsp; &nbsp;&quot;</span><span class="c7 c3">, </span><span class="c40 c3">&quot;| &nbsp; &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp;&quot;</span><span class="c7 c3">, </span><span class="c40 c3">&quot;| &nbsp; &nbsp; /|\ &nbsp; &nbsp; &quot;</span><span class="c7 c3">, </span><span class="c40 c3">&quot;| &nbsp; &nbsp; / \ &nbsp; &nbsp; &nbsp; </span></p><p class="c0 c10"><span class="c40 c3">&nbsp; &nbsp;&quot;</span><span class="c7 c3">, </span><span class="c40 c3">&quot;| &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &quot;</span><span class="c2">]</span></p><p class="c1 c23"><span class="c40 c3">&nbsp; &nbsp;</span><span class="c2">score_board = [</span><span class="c40 c3">&#39;__&#39;</span><span class="c2">] * </span><span class="c3 c25">len</span><span class="c2">(word)</span></p><p class="c1 c23"><span class="c40 c3">&nbsp; &nbsp;</span><span class="c2">win = </span><span class="c3 c25">False</span></p><p class="c1 c9"><span class="c2"></span></p><p class="c1 c23"><span class="c2">&nbsp; &nbsp;</span><span class="c7 c3 c5">print </span><span class="c40 c3">&#39;Welcome to Hangman</span></p><p class="c1 c23 c9"><span class="c40 c3"></span></p><p class="c1"><span class="c3">First we create </span><span class="c3">a function called hangman to hold our game. The word to guess will always start as &ldquo;cat&rdquo;, which we save in the variable </span><span class="c4">word</span><span class="c3">&nbsp;(you can change the word to guess by changing the variable). We use another variable </span><span class="c4">stage</span><span class="c3">&nbsp;to keep track of how many wrong answers the user has submitted. </span><span class="c4">stages</span><span class="c3">&nbsp;is a list filled with strings we will use to draw our Hangman. </span><span class="c4">scoreboard</span><span class="c3">&nbsp;is a list that keeps track of the hints we display to the user e.g., &ldquo;c __ t&rdquo;. The initial value of </span><span class="c4">score_board</span><span class="c3">&nbsp;is always &nbsp;</span><span class="c4">[&lsquo;__&rsquo;] * len(word)</span><span class="c3">&nbsp;which figures out the number of underscores to start out with. When the word is &ldquo;cat&rdquo; score_board starts as </span><span class="c4">[&ldquo;__&rdquo;, &ldquo;__&rdquo;, &ldquo;__&rdquo;]</span><span class="c15 c3">,</span><span class="c3">&nbsp;but it will adjust to whatever word is saved &nbsp;the </span><span class="c4">word</span><span class="c3">&nbsp;variable</span><span class="c15 c3">.</span><span class="c3">&nbsp;Finally, we have a </span><span class="c4">win</span><span class="c3">&nbsp;variable to keep track of whether the player has won the game yet or not. In the next step, we add a loop to our game: &nbsp; </span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c23"><span class="c3 c25">&nbsp; &nbsp;</span><span class="c7 c3 c5">while </span><span class="c2">stage &lt; </span><span class="c3 c25">len</span><span class="c2">(stages) - </span><span class="c3 c20">1</span><span class="c2">:</span></p><p class="c1 c23"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c7 c3 c5">print </span><span class="c40 c3">&#39;</span><span class="c7 c3">\n</span><span class="c40 c3">&#39;</span></p><p class="c1 c23 c9"><span class="c40 c3"></span></p><p class="c1"><span class="c3">When you build a game, it usually needs a loop to keep the game going until it&rsquo;s over. When the game is over, we break out of the loop. In this case, the game ends when the variable </span><span class="c4">stage</span><span class="c3">&nbsp;is equal to five, &nbsp; &nbsp;(there are 6 strings in our stages list but remember counting starts at 0). Instead of coding 5 into our loop (i.e., </span><span class="c4">while stage &lt; 5</span><span class="c3">), we use </span><span class="c4">len(stages) -1</span><span class="c3">&nbsp;so our code will still work if we add more stages. The reason we subtract 1 is because the </span><span class="c4">length()</span><span class="c3">&nbsp;function in Python does not count from 0, it counts from 1 (confusing I know). In order to compensate for this, we have to subtract one, to essentially make </span><span class="c4">length()</span><span class="c3">&nbsp;count starting from 0. In the next part of our program, we collect the user&rsquo;s guess:</span></p><p class="c1 c9"><span class="c40 c3"></span></p><p class="c0"><span class="c2">guess = </span><span class="c3 c25">input</span><span class="c2">(</span><span class="c40 c3">&quot;Guess a letter&quot;</span><span class="c2">)</span></p><p class="c0"><span class="c7 c3 c5">if </span><span class="c2">guess </span><span class="c7 c3 c5">in </span><span class="c2">word:</span></p><p class="c0"><span class="c2">&nbsp; &nbsp; score_board[word.index(guess)] = guess</span></p><p class="c0"><span class="c7 c3 c5">else</span><span class="c2">:</span></p><p class="c0"><span class="c2">&nbsp; &nbsp; stage += </span><span class="c3 c20">1</span></p><p class="c0 c9"><span class="c3 c20"></span></p><p class="c1"><span class="c3">Each time through our loop, we save the user&#39;s guess in the variable </span><span class="c4">guess</span><span class="c3">. If the letter they guess is in the word (which we check with the </span><span class="c4">in</span><span class="c3">&nbsp;keyword), we replace an underscore with the correct letter in our </span><span class="c4">scoreboard</span><span class="c3">&nbsp;list. Now when the </span><span class="c4">score_board</span><span class="c3">&nbsp;list is printed, it will show the letter they got right. We are able to replace the corresponding underscore using the built-in function, </span><span class="c4">index()</span><span class="c3">&nbsp;which gives us the index of a character in a string. So if the guess was &ldquo;a&rdquo;, and are word is &ldquo;cat&rdquo;, </span><span class="c4">&ldquo;cat&rdquo;.index(&ldquo;a&rdquo;)</span><span class="c3">&nbsp;would return index 1. If this is the user&rsquo;s first guess, our </span><span class="c4">score_board</span><span class="c3">&nbsp;list will initially look like this </span><span class="c4">[&ldquo;__&rdquo;, &ldquo;__&rdquo;, &ldquo;__&rdquo;]</span><span class="c3">, but after we replace index 1 in our </span><span class="c4">score_board</span><span class="c3">&nbsp;list with the correct guess (&ldquo;a&rdquo;) , our </span><span class="c4">scoreboard</span><span class="c3">&nbsp;list looks like this </span><span class="c4">[&ldquo;__&rdquo;, &ldquo;a&rdquo;, &ldquo;__&rdquo;]</span><span class="c3">. &nbsp;However, if the user guesses incorrectly, we increment the </span><span class="c4">stage</span><span class="c15 c3">&nbsp;</span><span class="c3">variable</span><span class="c3">&nbsp;by one, coming one step closer to ending the game. Once this is taken care of we simply print the scoreboard for this round using </span><span class="c4">join(</span><span class="c4">)</span><span class="c3">, and print our Hangman for the round.</span></p><p class="c1 c9"><span class="c3 c20"></span></p><p class="c0"><span class="c7 c3 c5">&nbsp;print</span><span class="c2">(</span><span class="c40 c3">&#39; &#39;</span><span class="c2">.join(score_board))</span></p><p class="c0"><span class="c2">&nbsp;</span><span class="c7 c3 c5">print </span><span class="c40 c3">&#39;</span><span class="c7 c3">\n</span><span class="c40 c3">&#39;</span><span class="c2">.join(stages[</span><span class="c3 c20">0</span><span class="c2">: stage + </span><span class="c3 c20">1</span><span class="c2">])</span></p><p class="c1 c9"><span class="c2"></span></p><p class="c1"><span class="c3">Printing our Hangman is a little tricky, we want each of the strings in our </span><span class="c4">stages</span><span class="c3">&nbsp;list to be printed on a new line, which is why we do a join with &lsquo;\n&rsquo;, which represents a new line. We also only want to print whatever stage of the Hangman we are on, which we accomplish by slicing our </span><span class="c4">stages</span><span class="c3">&nbsp;list. We start at </span><span class="c4">stage</span><span class="c4">&nbsp;</span><span class="c4">0</span><span class="c3">&nbsp;and slice up until whatever stage we are at plus one. The reason we add one, is because when you are slicing, the end slice does not get included in the results. The last thing we do in our loop is check if &nbsp;the user has won the game:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c0"><span class="c7 c3 c5">if </span><span class="c40 c3">&#39;__&#39; </span><span class="c7 c3 c5">not in </span><span class="c2">score_board:</span></p><p class="c0"><span class="c7 c3 c5">&nbsp; &nbsp;</span><span class="c2">&nbsp;</span><span class="c7 c3 c5">print </span><span class="c40 c3">&#39;You win! The word was:&#39;</span></p><p class="c0"><span class="c40 c3">&nbsp; &nbsp; </span><span class="c7 c3 c5">print</span><span class="c2">(</span><span class="c40 c3">&#39; &#39;</span><span class="c2">.join(score_board))</span></p><p class="c0"><span class="c2">&nbsp; &nbsp; win = </span><span class="c3 c25">True</span></p><p class="c1"><span class="c3 c25">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c7 c3 c5">break</span><span class="c3">&nbsp;</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">If there are no more </span><span class="c3">&ldquo;</span><span class="c3">__</span><span class="c3">&rdquo;</span><span class="c3">&rsquo;s in the score_board, we know the user has guessed all the letters and won. If the user won, we print that they won and we print the </span><span class="c4">scoreboard</span><span class="c3">&nbsp;list. We also set the variable </span><span class="c4">win</span><span class="c3">&nbsp;to </span><span class="c4">True</span><span class="c3">&nbsp;for later use and break out of our loop.</span></p><p class="c0"><span class="c3">Once we break out of our loop, if the user won, we do nothing. However, if the user did not win, the variable win will be set to </span><span class="c4">False</span><span class="c15 c3">&nbsp;</span><span class="c3">. If that is the case, we print the full Hangman and print &ldquo;You lose!&rdquo;. </span></p><p class="c1 c9"><span class="c3"></span></p><p class="c0"><span class="c7 c3 c5">if not </span><span class="c2">win:</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c3 c5 c7">print </span><span class="c40 c3">&#39;</span><span class="c7 c3">\n</span><span class="c40 c3">&#39;</span><span class="c2">.join(stages[</span><span class="c3 c20">0</span><span class="c2">: stage])</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c7 c3 c5">print </span><span class="c40 c3">&#39;You lose!&#39;</span></p><p class="c1 c9 c10"><span class="c40 c3"></span></p><p class="c0"><span class="c2">hangman()</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">Here is our complete code:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# </span><span class="c17 c3"><a class="c18" href="https://www.google.com/url?q=https://github.com/calthoff/tstp/blob/master/part_I/bringing_it_all_together/hangman.py&amp;sa=D&amp;ust=1467337426377000&amp;usg=AFQjCNHyHXhm3mXF4PEf1Ob4bYT5rk4pXA">https://github.com/calthoff/tstp/blob/master/part_I/bringing_it_all_together/hangman.py</a></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c7 c3 c5">def </span><span class="c2 c5">hangman</span><span class="c2">():</span></p><p class="c1 c23"><span class="c2">&nbsp; &nbsp;stage = </span><span class="c3 c20">0</span></p><p class="c0 c10"><span class="c3 c20">&nbsp; &nbsp;</span><span class="c2">stages = [</span><span class="c40 c3">&quot;&quot;</span><span class="c7 c3">, </span><span class="c40 c3">&quot;________ &nbsp; &nbsp; &nbsp;&quot;</span><span class="c7 c3">, </span><span class="c40 c3">&quot;| &nbsp; &nbsp; &nbsp;| &nbsp; &nbsp; &nbsp;&quot;</span><span class="c7 c3">, </span><span class="c40 c3">&quot;| &nbsp; &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp;</span><span class="c40 c3">&quot;</span><span class="c7 c3">, </span><span class="c40 c3">&quot;</span><span class="c40 c3">| &nbsp; &nbsp; /|\ &nbsp; &nbsp; &quot;</span><span class="c7 c3">, </span><span class="c40 c3">&quot;| &nbsp; &nbsp; / \ &nbsp; &nbsp; &nbsp; </span></p><p class="c0 c10"><span class="c40 c3">&nbsp; &nbsp;&quot;</span><span class="c7 c3">, </span><span class="c40 c3">&quot;| &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &quot;</span><span class="c2">]</span></p><p class="c1 c23"><span class="c2">&nbsp; &nbsp;word = </span><span class="c40 c3">&quot;cat&quot;</span></p><p class="c1 c23"><span class="c40 c3">&nbsp; &nbsp;</span><span class="c2">score_board = [</span><span class="c40 c3">&#39;__&#39;</span><span class="c2">] * </span><span class="c3 c25">len</span><span class="c2">(word)</span></p><p class="c1 c23"><span class="c40 c3">&nbsp; &nbsp;</span><span class="c2">win = </span><span class="c3 c25">False</span></p><p class="c1 c9"><span class="c2"></span></p><p class="c1 c23"><span class="c2">&nbsp; &nbsp;</span><span class="c7 c3 c5">print </span><span class="c40 c3">&#39;Welcome to Hangman&#39;</span></p><p class="c1 c9"><span class="c3 c25"></span></p><p class="c1 c23"><span class="c3 c25">&nbsp; &nbsp;</span><span class="c7 c3 c5">while </span><span class="c2">stage &lt; </span><span class="c3 c25">len</span><span class="c2">(stages) - </span><span class="c3 c20">1</span><span class="c2">:</span></p><p class="c1 c23"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c7 c3 c5">print </span><span class="c40 c3">&#39;</span><span class="c7 c3">\n</span><span class="c40 c3">&#39;</span></p><p class="c1 c23"><span class="c40 c3">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c2">guess = </span><span class="c3 c25">input</span><span class="c2">(</span><span class="c40 c3">&quot;Guess a letter&quot;</span><span class="c2">)</span></p><p class="c1 c23"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c7 c3 c5">if </span><span class="c2">guess </span><span class="c7 c3 c5">in </span><span class="c2">word:</span></p><p class="c1 c23"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;score_board[word.index(guess)] = guess</span></p><p class="c1 c23"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c7 c3 c5">else</span><span class="c2">:</span></p><p class="c1 c23"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;stage += </span><span class="c3 c20">1</span></p><p class="c1 c23"><span class="c3 c20">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c7 c3 c5">print</span><span class="c2">(</span><span class="c40 c3">&#39; &#39;</span><span class="c2">.join(score_board))</span></p><p class="c1 c23"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c7 c3 c5">print </span><span class="c40 c3">&#39;</span><span class="c7 c3">\n</span><span class="c40 c3">&#39;</span><span class="c2">.join(stages[</span><span class="c3 c20">0</span><span class="c2">: stage + </span><span class="c3 c20">1</span><span class="c2">])</span></p><p class="c1 c23"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c7 c3 c5">if </span><span class="c40 c3">&#39;__&#39; </span><span class="c7 c3 c5">not in </span><span class="c2">score_board:</span></p><p class="c1 c23"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c7 c3 c5">print </span><span class="c40 c3">&#39;You win! The word was:&#39;</span></p><p class="c1 c23"><span class="c40 c3">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c7 c3 c5">print</span><span class="c2">(</span><span class="c40 c3">&#39; &#39;</span><span class="c2">.join(score_board))</span></p><p class="c1 c23"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;win = </span><span class="c3 c25">True</span></p><p class="c1 c23"><span class="c3 c25">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c7 c3 c5">break</span></p><p class="c1 c23 c9"><span class="c7 c3 c5"></span></p><p class="c1 c23"><span class="c7 c3 c5">&nbsp; &nbsp;if not </span><span class="c2">win:</span></p><p class="c1 c23"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c7 c3 c5">print </span><span class="c40 c3">&#39;</span><span class="c7 c3">\n</span><span class="c40 c3">&#39;</span><span class="c2">.join(stages[</span><span class="c3 c20">0</span><span class="c2">: stage])</span></p><p class="c1 c23"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c7 c3 c5">print </span><span class="c40 c3">&#39;You lose!&#39;</span></p><p class="c1 c23 c9"><span class="c40 c3"></span></p><p class="c1 c23"><span class="c2">hangman()</span></p><p class="c1 c23 c9"><span class="c2 c11"></span></p><h3 class="c1 c13" id="h.wqrs4kmfcq82"><span>Wrapping Up</span></h3><p class="c1"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">Building text-based games is a great way to improve your programming ability. Now you have all the tools to build as many games as you&rsquo;d like. </span></p><h3 class="c1 c13" id="h.rj15pg5f4vod"><span>Challenge</span></h3><p class="c1"><span class="c3">Build another text-based game that interests you. </span></p><h2 class="c1 c13" id="h.ctkubigcafm3"><span>Chapter 1</span><span>2</span><span>. </span><span>Practice</span></h2><p class="c0"><span class="c3">If this is your first programming book, I recommend you spend a lot of time practicing before moving on to the next section. In this chapter, I provide exercises to help you get some practice before moving on to the next section, resources to check out and we cover how to get help you if you get stuck. </span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.11bmgoljb6tg"><span>Exercises </span></h3><ol class="c45 lst-kix_q0c03zlevnjq-0 start" start="1"><li class="c1 c10 c38"><span class="c3">Create a text based game of your favorite sport. </span></li><li class="c1 c10 c38"><span class="c3">Make up your own text based game. When I was starting out I built a fantasy based game based on Heroes of Might and Magic III</span></li><li class="c1 c10 c38"><span class="c3">Build a text based magic 8 ball program where the user can shake a magic eight ball and get predictions about their future</span></li><li class="c1 c10 c38"><span class="c3">Create a program that asks the user what kind of mood they are in and recommends a song. </span></li><li class="c1 c10 c38"><span class="c3">Build a program that prints out ten brands and lets the user type them in. When they do, the program should print the brand&rsquo;s trademark i</span><span class="c3">.</span><span class="c3">e.</span><span class="c3">,</span><span class="c3">&nbsp;the user could type Nike and the program would print &ldquo;Just do it.&rdquo;</span></li></ol><h3 class="c1 c13" id="h.9kluowq1vpg2"><span>Read</span></h3><ol class="c45 lst-kix_unuqtgbahdmw-0 start" start="1"><li class="c1 c10 c38"><span class="c17 c3"><a class="c18" href="https://www.google.com/url?q=http://programmers.stackexchange.com/questions/44177/what-is-the-single-most-effective-thing-you-did-to-improve-your-programming-skil&amp;sa=D&amp;ust=1467337426404000&amp;usg=AFQjCNFIOuz96Vd8Zb3a86U4txrOF-GtVw">http://programmers.stackexchange.com/questions/44177/what-is-the-single-most-effective-thing-you-did-to-improve-your-programming-skil</a></span></li><li class="c1 c10 c38"><span class="c17 c3"><a class="c18" href="https://www.google.com/url?q=https://www.codementor.io/ama/0926528143/stackoverflow-python-moderator-martijn-pieters-zopatista&amp;sa=D&amp;ust=1467337426405000&amp;usg=AFQjCNG-FRibxieeZRwnq_esh8MsbBhStw">https://www.codementor.io/ama/0926528143/stackoverflow-python-moderator-martijn-pieters-zopatista</a></span></li></ol><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.apk3pslpmgco"><span>Getting Help</span></h3><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;If you get stuck, I recommend checking out Stack Exchange&mdash;an amazing resource for programmers. There are two websites on Stack Exchange you should explore. The first is Stack Overflow. If you haven&rsquo;t used it yet, you will soon be familiar. Google almost any programming question and an answer will pop up on Stackoverflow, which makes it a complete game changer for learning to program. </span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;In fact, learning to rely on Stack Overflow was an important lesson for me. </span><span class="c3">Struggling to figure things out is a major part of the learning process, but at some point it becomes counter</span><span class="c3">-</span><span class="c3">productive</span><span class="c3">. Working on projects in the past, I used to continue to struggle way past the point where it was productive. Today if that happens, I will post a question on Stack Overflow, if the answer is not already on there. Every time I&rsquo;ve posted a question, someone has been able to answer it. I can&rsquo;t say enough about how helpful and friendly the Stack Overflow community is. </span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Code Review on Stack Exchange is another resource that can make a big difference in your programming journey. Anyone can go on Code Review and post their code. Other members of the Stack Exchange community then review your code and give you feedback regarding what you did well and offer helpful suggestions on how you can improve. </span></p><h1 class="c1 c13" id="h.nznwuon0i2sk"><span>Part II Learn to Program with Objects</span></h1><p class="c1 c9"><span></span></p><h2 class="c1 c13" id="h.u3vnqd9y9yck"><span>Chapter 13. &nbsp;</span><span>Object-oriented Programming</span></h2><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Now that you can create programs, it&rsquo;s time to start thinking of how they should be structured. &nbsp;Object-oriented programming is a programming paradigm, or a specific way of programming that helps give your programs structure. Object-oriented programming involves designing classes to create objects that interact with each other. In this chapter, we learn how to program using classes and objects.</span></p><h3 class="c1 c13" id="h.n2243pisgegu"><span>Everything is an Object</span></h3><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;It turns out you&rsquo;ve already been programming with objects, because everything in Python is an object. An object is an instance of a class. Classes are the blueprint used to create objects. You can think of a class as the idea of an object. Think of an orange. An orange is an object. A fruit weighing between 2 to 10 ounces is the idea of an orange, or a class. To recap, an actual orange is an object, whereas the description that defines an orange is a class. Here is an example of a class defining an orange in Python: </span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p><p class="c1"><span class="c3"># https://github.com/calthoff/tstp/blob/master/part_II/object_oriented_programming/orange_ex1.py</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c10"><span class="c7 c3 c5">class </span><span class="c2 c5">Orange</span><span class="c2">:</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp;</span><span class="c7 c3 c5">def &nbsp;</span><span class="c29 c3">__init__</span><span class="c2">(</span><span class="c3 c8">self</span><span class="c2">):</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c3 c8">self</span><span class="c2">.weight = </span><span class="c3 c20">6</span></p><p class="c1 c10"><span class="c3 c20">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c3 c8">self</span><span class="c2">.color = </span><span class="c12 c3">&#39;orange&#39;</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">Using this definition, we can create as many orange objects as we&rsquo;d like. </span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">&nbsp; &nbsp; </span><span class="c2">orange</span><span class="c2">&nbsp;= Orange()</span></p><p class="c1 c10"><span class="c3 c25">&nbsp; &nbsp; </span><span class="c3 c25">print</span><span class="c2">(</span><span class="c2">orange</span><span class="c2">.weight)</span></p><p class="c1 c10"><span class="c3 c25">&nbsp; &nbsp; </span><span class="c3 c25">print</span><span class="c2">(</span><span class="c2">orange</span><span class="c2">.color)</span></p><p class="c1 c10"><span class="c3 c25">&nbsp; &nbsp; </span><span class="c3 c25">print</span><span class="c2">(</span><span class="c2">orange</span><span class="c2">)</span></p><p class="c1 c10"><span class="c3 c25">&nbsp; &nbsp; </span><span class="c3 c25">print</span><span class="c2">(</span><span class="c3 c25">type</span><span class="c2">(</span><span class="c2">orange</span><span class="c2">)</span><span class="c2">)</span></p><p class="c1 c9"><span class="c4"></span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c4">&gt;&gt; 6</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; &lsquo;orange&rsquo;</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; </span><span class="c4">&lt;__main__.Orange object at 0x10564dba8&gt;</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; &lt;class &#39;__main__.Orange&#39;&gt;</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">We created a new </span><span class="c4">Orange</span><span class="c3">&nbsp;object using the syntax </span><span class="c4">Orange()</span><span class="c3">. This is called instantiating an object, which means creating a new object. You instantiate an object by adding parenthesis to the class name. As you can see, the orange object has all of the variables defined by our class: weight and color. When we print our Orange object, Python lets us know it is an Orange object, and the memory location the object is stored at (don&rsquo;t worry about that for now). Furthermore, when we print type(orange), Python lets us know it is an instance of the </span><span class="c4">Orange</span><span class="c3">&nbsp;class. &nbsp; &nbsp; </span></p><p class="c0"><span class="c3">In our example, you will notice there is what looks like a function called </span><span class="c4">__init__ </span><span class="c3">(don&rsquo;t forget there are two underscores before and after). &nbsp;This is a method&mdash;which is like a function, except it is part of a class. These </span><span class="c3">methods </span><span class="c3">have access to all of the orange object&rsquo;s variables. </span><span class="c4">__init__</span><span class="c3">&nbsp; is a built-in method every class comes with, and it&rsquo;s used to initialize an object whenever it is created. &nbsp;</span><span class="c4">__init__ </span><span class="c3">gets passed a parameter called </span><span class="c4">self</span><span class="c3">. </span><span class="c3">The parameter </span><span class="c4">self </span><span class="c3">refers to the object created when the class gets initialized. This concept exists in most programming languages that support object-oriented programming, however, Python makes </span><span class="c4">self</span><span class="c3">&nbsp;explicit whereas many other languages make it implicit. What I mean is that Python makes you explicitly pass in </span><span class="c4">self</span><span class="c3">&nbsp;to every method that wants to reference the object, whereas in other languages </span><span class="c4">self</span><span class="c3">&nbsp;is just implied to have been passed to the object. </span></p><p class="c0 c9"><span class="c3"></span></p><p class="c0"><span class="c3">[add chart/illustration explaining Object-oriented programming.]</span></p><p class="c0 c9"><span class="c3"></span></p><p class="c0"><span class="c3">That was a lot to take in</span><span class="c3">, so let&#39;s go over it all one more time. In Object-oriented programming, we use classes to model objects. These objects group variables and functions (methods) together in a single unit&mdash;the object. Everything in Python is an object. Classes have a special method called </span><span class="c4">__init__</span><span class="c3">&nbsp;that Python calls when an object is created using the syntax </span><span class="c4">NewObject()</span><span class="c3">. When you create a new </span><span class="c4">orange()</span><span class="c3">&nbsp;object, Python calls the special </span><span class="c4">__init__</span><span class="c3">&nbsp; method and initializes your new object, and executes whatever other code is in your </span><span class="c4">__init__</span><span class="c3">&nbsp;method. The first argument to </span><span class="c4">__init__</span><span class="c3">&nbsp;is called </span><span class="c4">self </span><span class="c3">(it actually can be named anything, but naming the first argument </span><span class="c4">self</span><span class="c3">&nbsp;is a Python programming convention I&rsquo;ve never seen broken)</span><span class="c3">&nbsp;</span><span class="c3">and it references the object you are going to create in the future. </span><span class="c4">&nbsp;</span><span class="c3">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span></p><p class="c0"><span class="c3">Moving forward with the modeling of our orange&mdash;there is more to an orange than just its physical attributes. Oranges can also do things, and we need to be able to model that as well. What can an orange do? Well, for one thing, oranges can go bad from mold. We model things an object can do with methods.We can model our molding orange by adding a mold attribute to our Orange class, and creating a method that increases the amount of mold in our orange:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3"># https://github.com/calthoff/tstp/blob/master/part_II/object_oriented_programming/orange_ex2.py</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c10"><span class="c7 c3 c5">class </span><span class="c2 c5">Orange</span><span class="c2">():</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp;</span><span class="c7 c3 c5">def </span><span class="c29 c3">__init__</span><span class="c2">(</span><span class="c3 c8">self</span><span class="c2">):</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c3 c8">self</span><span class="c2">.weight = </span><span class="c3 c20">6</span></p><p class="c1 c10"><span class="c3 c20">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c3 c8">self</span><span class="c2">.color = </span><span class="c12 c3">&#39;orange&#39;</span></p><p class="c1 c10"><span class="c12 c3">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c3 c8">self</span><span class="c2">.mold = </span><span class="c3 c20">0</span></p><p class="c1 c9 c10"><span class="c3 c20"></span></p><p class="c1 c10"><span class="c3 c20">&nbsp; &nbsp;</span><span class="c7 c3 c5">def </span><span class="c2 c5">rot</span><span class="c2">(</span><span class="c3 c8">self</span><span class="c7 c3">, </span><span class="c2">days</span><span class="c7 c3">, </span><span class="c2">temperature):</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c3 c8">self</span><span class="c2">.mold = days * (temperature * </span><span class="c3 c20">.1</span><span class="c2">)</span></p><p class="c1 c9 c10"><span class="c2"></span></p><p class="c1 c10"><span class="c2">orange = Orange()</span></p><p class="c1 c10"><span class="c3 c25">print</span><span class="c2">(orange.mold)</span></p><p class="c1 c10"><span class="c2">orange.rot(</span><span class="c3 c20">10</span><span class="c7 c3">, </span><span class="c3 c20">98</span><span class="c2">)</span></p><p class="c1 c10"><span class="c3 c25">print</span><span class="c2">(orange.mold)</span></p><p class="c1 c9"><span class="c4"></span></p><p class="c0"><span class="c4">&gt;&gt; 0</span></p><p class="c1 c10"><span class="c4">&gt;&gt; 98.0</span><span class="c4">&nbsp;</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">Now our orange objects will be able to rot. We defined a method allowing us to pass in the number of </span><span class="c3">days </span><span class="c3">it&#39;s been since the orange was picked, and the average temperature during that time. With our made</span><span class="c3">-</span><span class="c3">up formula, we can increase the amount of mold the orange has every time we call the rot method; our orange now has the ability to rot.</span></p><p class="c0"><span class="c3">We can change our class definition so the person creating the object can pass in their own variables when they create a new orange, instead of the weight, color and mold starting with default values . Here is our new class definition:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3"># https://github.com/calthoff/tstp/blob/master/part_II/object_oriented_programming/orange_ex3.py</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c10"><span class="c7 c3 c5">class </span><span class="c2 c5">Orange</span><span class="c2">:</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp;</span><span class="c7 c3 c5">def </span><span class="c29 c3">__init__</span><span class="c2">(</span><span class="c3 c8">self</span><span class="c7 c3">, </span><span class="c2">weight</span><span class="c7 c3">, </span><span class="c2">color</span><span class="c7 c3">, </span><span class="c2">mold):</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c3 c30">&quot;&quot;&quot;all weights are in oz&quot;&quot;&quot;</span></p><p class="c1 c10"><span class="c3 c30">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c3 c8">self</span><span class="c2">.weight = weight</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c3 c8">self</span><span class="c2">.color = color</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c3 c8">self</span><span class="c2">.mold = mold</span></p><p class="c1 c9 c10"><span class="c2"></span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp;</span><span class="c7 c3 c5">def </span><span class="c2 c5">rot</span><span class="c2">(</span><span class="c3 c8">self</span><span class="c7 c3">, </span><span class="c2">days</span><span class="c7 c3">, </span><span class="c2">temperature):</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c3 c8">self</span><span class="c2">.mold = days * (temperature * </span><span class="c3 c20">.1</span><span class="c2">)</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">Now we can create a wider variety of oranges objects. Here is the syntax to create a new orange from our new </span><span class="c4">Orange</span><span class="c3">&nbsp;class:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c0"><span class="c2">orange</span><span class="c2">&nbsp;= Orange(</span><span class="c3 c20">10</span><span class="c7 c3">, </span><span class="c12 c3">&#39;orange&#39;</span><span class="c7 c3">, </span><span class="c3 c20">2</span><span class="c2">)</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">We just created a moldy, 10 oz (per the comment &ldquo;all weight are in oz&rdquo;), &nbsp;orange colored orange. We can access these variables using dot notation:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3 c8 c5">print</span><span class="c2">(orange.weight)</span></p><p class="c1 c9"><span class="c4"></span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; 10</span></p><p class="c1 c9"><span class="c4"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3 c8 c5">print</span><span class="c2">(orange.color)</span></p><p class="c1 c9"><span class="c4"></span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; &ldquo;orange&rdquo;</span></p><p class="c1 c9"><span class="c4"></span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3 c8 c5">print</span><span class="c2">(orange.mold)</span></p><p class="c1 c9"><span class="c4"></span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; 2</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">We can also change any of the values of our orange object:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c2">orange</span><span class="c2">.weight = </span><span class="c3 c20">100</span></p><p class="c0"><span class="c3 c25">print</span><span class="c2">(</span><span class="c2">orange</span><span class="c2">.weight)</span></p><p class="c1 c9"><span class="c4"></span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; 100</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;And we can further rot our orange by calling the rot method:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c2">orange</span><span class="c2">.rot(</span><span class="c3 c20">50, 100</span><span class="c2">)</span></p><p class="c0"><span class="c3 c25">print</span><span class="c2">(</span><span class="c2">orange</span><span class="c2">.mold)</span></p><p class="c1 c9"><span class="c4"></span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c4">&gt;&gt; 500.0</span><sup><a href="#cmnt17" id="cmnt_ref17">[q]</a></sup></p><p class="c1 c9 c10"><span class="c3"></span></p><h3 class="c1 c13" id="h.nbpycovbjrmt"><span>Wrapping Up</span></h3><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;This is some of the most difficult programming we&rsquo;ve done so far. Don&rsquo;t worry if you get frustrated with these concepts at first. I recommend spending at least thirty</span><span class="c3">&nbsp;minutes </span><span class="c3">reading and rereading the code from this chapter. </span></p><p class="c1 c9"><span></span></p><h3 class="c1 c13" id="h.2yz4z742ssif"><span>Challenge</span></h3><p class="c1"><span>C</span><span class="c3">reate a new Python file, and recreate modeling an Orange as many times as you have to until you can create an Orange object </span><span class="c3">without referencing </span><span class="c3">this chapter. </span></p><p class="c1 c9"><span></span></p><h2 class="c1 c13" id="h.fcj5558yrccw"><span>Chapter 14. More </span><span>Object-oriented Programming</span></h2><h3 class="c1 c13" id="h.7k3bjvzeybrm"><span>Assignment</span></h3><p class="c1"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">As I mentioned in the last chapter, everything in Python is an object. You can think of every variable in Python as pointing to an object. </span></p><p class="c1 c9"><span class="c3"></span></p><p class="c0"><span class="c4">number = 100</span></p><p class="c0 c9"><span class="c3"></span></p><p class="c1"><span class="c3">In this example, </span><span class="c4">number</span><span class="c3">&nbsp;points to an integer object. </span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c4">number = 101</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">When we change a variable, it points to a new object. </span><span class="c4">number</span><span class="c3">&nbsp;now points to a new integer object.</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;Two variables can also point to the same object. </span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x = 100</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;y = x</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">In this case, </span><span class="c4">x </span><span class="c3">points to an integer object, and </span><span class="c4">y</span><span class="c3">&nbsp;points to the same integer object. </span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">[insert illustration from </span><span class="c17 c3"><a class="c18" href="https://www.google.com/url?q=http://cdn.oreillystatic.com/en/assets/1/event/95/Python%2520103_%2520Memory%2520Model%2520_%2520Best%2520Practices%2520Presentation.pdf&amp;sa=D&amp;ust=1467337426471000&amp;usg=AFQjCNEPIO9BLpFZbbvqwWByHmJk9jEtUQ">http://cdn.oreillystatic.com/en/assets/1/event/95/Python%20103_%20Memory%20Model%20_%20Best%20Practices%20Presentation.pdf</a></span><span class="c3">]</span><sup><a href="#cmnt18" id="cmnt_ref18">[r]</a></sup></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">What do you think the following program </span><span class="c3">will </span><span class="c3">print?</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x = 10</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;y = x</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x += 1</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(x)</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(y) </span></p><p class="c1 c9"><span class="c4"></span></p><p class="c1"><span class="c3">The answer is 11 and 10. </span><span class="c4">x</span><span class="c3">&nbsp;points to an integer object representing the number 10, and y points to the same integer object. </span><span class="c3">When we increment </span><span class="c4">x</span><span class="c3">, </span><span class="c4">x</span><span class="c3">&nbsp;points to a new integer object&mdash;11, but this does not change the fact that </span><span class="c4">y</span><span class="c3">&nbsp;still points to the integer object 10</span><span class="c3">. What do you think the output of this example will be?</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c0"><span class="c4">x = [1, 2, 3]</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;y = x</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;y[2] = 100</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(x)</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(y)</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">The output will be </span><span class="c3">[1, 2, 100] twice</span><span class="c3">. The reason is that both x and y point to the same list object. In the third line, we make a change to that single list object, and when we print both variables, it prints the list object they both point to, with the changes made in line 3. </span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p><h3 class="c1 c13" id="h.u9osed5xbgtc"><span>i</span><span>s</span></h3><p class="c0"><span class="c3">While </span><span class="c4">==</span><span class="c3">&nbsp;is used to check for equality, the keyword</span><span class="c4">&nbsp;is </span><span class="c3">checks if two variables are the same object.</span></p><p class="c0 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&ldquo;&ldquo;&ldquo; https://github.com/calthoff/tstp/blob/master/part_II/more_object_oriented_programming/is_example.py</span></p><p class="c1"><span class="c3">&nbsp;&rdquo;&rdquo;&rdquo;</span></p><p class="c0 c9"><span class="c3"></span></p><p class="c0"><span class="c7 c3 c5">class </span><span class="c2 c5">Person</span><span class="c2">:</span></p><p class="c0"><span class="c2">&nbsp; &nbsp;</span><span class="c7 c3 c5">def </span><span class="c29 c3">__init__</span><span class="c2">(</span><span class="c3 c8">self</span><span class="c2">):</span></p><p class="c0"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c3 c8">self</span><span class="c2">.name = </span><span class="c12 c3">&#39;Bob&#39;</span></p><p class="c0 c9"><span class="c12 c3"></span></p><p class="c0"><span class="c2">bob = Person()</span></p><p class="c0"><span class="c2">bob2 = bob</span></p><p class="c0"><span class="c3 c25">print</span><span class="c2">(bob </span><span class="c7 c3 c5">is </span><span class="c2">bob2)</span></p><p class="c0 c9"><span class="c2"></span></p><p class="c0"><span class="c2">another_bob = Person()</span></p><p class="c0"><span class="c3 c25">print</span><span class="c2">(bob </span><span class="c7 c3 c5">is </span><span class="c2">another_bob)</span></p><p class="c1 c9"><span class="c4"></span></p><p class="c0"><span class="c4">&gt;&gt; True</span></p><p class="c0"><span class="c4">&gt;&gt; False</span></p><p class="c0 c9"><span class="c4"></span></p><h3 class="c1 c13" id="h.l8wdd4brgtyy"><span>Class Variables</span></h3><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Classes can have two different types of variables&mdash;instance variables and class variables. The variables we&rsquo;ve seen so far&mdash;defined using self&mdash;are instance variables; they belong to the object when it is created, whereas class variables are used by the class and the object. This doesn&rsquo;t make sense&mdash;until you remember that </span><span class="c15 c3">everything</span><span class="c3">&nbsp;in Python is an object. Yes, in Python, even classes are objects. If we try to print </span><span class="c4">Person</span><span class="c3">&nbsp;from the last example&mdash;it will work, because Person is a class object that Python creates when it executes our code.</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c0"><span class="c3">print(Person)</span></p><p class="c0"><span class="c3">&gt;&gt; &lt;class &#39;__main__.Person&#39;&gt; </span><span class="c3">&nbsp; &nbsp; </span></p><p class="c0 c9"><span class="c3"></span></p><p class="c1"><span class="c3">We can create class variables like this:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&ldquo;&ldquo;&ldquo; </span></p><p class="c1"><span class="c17 c3"><a class="c18" href="https://www.google.com/url?q=https://github.com/calthoff/tstp/blob/master/part_II/more_object_oriented_programming/class_variables_ex1.py&amp;sa=D&amp;ust=1467337426489000&amp;usg=AFQjCNHjvkR-bclmXI8wEOeQ7ob3ca31tQ">https://github.com/calthoff/tstp/blob/master/part_II/more_object_oriented_programming/class_variables_ex1.py</a></span></p><p class="c1"><span class="c3">&nbsp;&rdquo;&rdquo;&rdquo;</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c0"><span class="c7 c3 c5">class </span><span class="c2 c5">Person</span><span class="c2">:</span></p><p class="c0"><span class="c2">&nbsp; &nbsp;people_list = [] &nbsp;</span></p><p class="c0"><span class="c2">&nbsp; &nbsp;</span><span class="c7 c3 c5">def </span><span class="c29 c3">__init__</span><span class="c2">(</span><span class="c3 c8">self</span><span class="c2">):</span></p><p class="c0"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c3 c8">self</span><span class="c2">.name = </span><span class="c12 c3">&#39;Bob&#39;</span></p><p class="c0"><span class="c76 c12 c3">&nbsp; &nbsp; &nbsp; </span></p><p class="c0"><span class="c3">&gt;&gt;</span></p><p class="c0 c9"><span class="c76 c12 c3"></span></p><p class="c1"><span class="c4">people_list</span><span class="c3">&nbsp;is a class variable because it&rsquo;s a variable inside of a class created without the syntax </span><span class="c4">self.[name] = [variable]</span><span class="c3">. We can use class variables to fill up a list with all the objects that have been created with our class:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&ldquo;&ldquo;&ldquo; </span></p><p class="c1"><span class="c3">https://github.com/calthoff/tstp/blob/master/part_II/more_object_oriented_programming/class_variables_ex2.py</span></p><p class="c1"><span class="c3">&nbsp;&rdquo;&rdquo;&rdquo;</span></p><p class="c0"><span class="c7 c3 c5">class </span><span class="c2 c5">Person</span><span class="c2">:</span></p><p class="c0"><span class="c2">&nbsp; &nbsp;people_list = []</span></p><p class="c0"><span class="c2">&nbsp; &nbsp;</span><span class="c7 c3 c5">def </span><span class="c29 c3">__init__</span><span class="c2">(</span><span class="c3 c8">self</span><span class="c7 c3">, </span><span class="c2">name):</span></p><p class="c0"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c3 c8">self</span><span class="c2">.name = name</span></p><p class="c0"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c3 c8">self</span><span class="c2">.people_list.append(name)</span></p><p class="c0 c9"><span class="c2"></span></p><p class="c0"><span class="c2">bob = Person(</span><span class="c12 c3">&#39;Ada Lovelace&#39;</span><span class="c2">)</span></p><p class="c0"><span class="c2">joe = Person(</span><span class="c12 c3">&#39;Grace Hopper&#39;</span><span class="c2">)</span></p><p class="c0"><span class="c2">jane = Person(</span><span class="c12 c3">&#39;Leah Culver&#39;</span><span class="c2">)</span></p><p class="c0"><span class="c3 c25">print</span><span class="c2">(Person.people_list)</span></p><p class="c1 c9"><span class="c4"></span></p><p class="c0"><span class="c4">&gt;&gt;</span><span class="c4">&nbsp;[&#39;Ada Lovelace&#39;, &#39;Grace Hopper&#39;, &#39;Leah Culver&#39;]</span></p><p class="c0 c9"><span class="c4"></span></p><p class="c1"><span class="c3">We define class variables like a regular variable in Python. Once</span><span class="c3">&nbsp;</span><span class="c3">defined</span><span class="c3">,</span><span class="c3">&nbsp;class variables </span><span class="c3">can be accessed </span><span class="c3">anywhere in your class with the same syntax as an instance variable, in this case: </span><span class="c4">self.people_list</span><span class="c3">. Use class variables when you want your class object to have access to information.</span></p><p class="c1 c9"><span class="c4"></span></p><h3 class="c1 c13" id="h.lwnc0kieeblu"><span>I</span><span>nheritance</span></h3><p class="c0"><span class="c43 c3">In the last chapter, I went over how Python classes &ldquo;start&rdquo; with an </span><span class="c43 c4">__init__</span><span class="c3 c43">&nbsp;method. </span><span class="c43 c3">The way this happens is actually an important programming concept called inheritance, and we will take &nbsp;a look at how it works in this section. </span><span class="c3">Inheritance in programming is similar to genetic inheritance</span><span class="c3">, </span><span class="c3">i.e., you can inherit attributes from your parents, like your eye color. </span><span class="c43 c3">When you create a class, </span><span class="c3">it can</span><span class="c43 c3">&nbsp;inherit from another class</span><span class="c3">&mdash;</span><span class="c43 c3">called a parent class</span><span class="c3">&mdash;giving the</span><span class="c43 c3">&nbsp;new class you </span><span class="c3">created</span><span class="c43 c3">&nbsp;access to the </span><span class="c3">variables</span><span class="c43 c3">&nbsp;and methods of the parent class. Our first ex</span><span class="c3">ample of a parent class will be a class representing an actual parent (the human version). </span><span class="c43 c3">&nbsp;</span></p><p class="c0 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&ldquo;&ldquo;&ldquo; </span></p><p class="c1"><span class="c3">https://github.com/calthoff/tstp/blob/master/part_II/more_object_oriented_programming/inheritance_ex1.py</span></p><p class="c1"><span class="c3">&nbsp;&rdquo;&rdquo;&rdquo;</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c10"><span class="c7 c3 c5">class </span><span class="c2 c5">Parent</span><span class="c2">():</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp;</span><span class="c7 c3 c5">def </span><span class="c29 c3">__init__</span><span class="c2">(</span><span class="c3 c8">self</span><span class="c2">):</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c3 c8">self</span><span class="c2">.name = </span><span class="c12 c3">&#39;Ricky&#39;</span></p><p class="c1 c10"><span class="c12 c3">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c3 c8">self</span><span class="c2">.eye_color = </span><span class="c12 c3">&#39;brown&#39;</span></p><p class="c1 c9 c10"><span class="c12 c3"></span></p><p class="c1 c10"><span class="c12 c3">&nbsp; &nbsp;</span><span class="c7 c3 c5">def </span><span class="c2 c5">print_name</span><span class="c2">(</span><span class="c3 c8">self</span><span class="c2">):</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c3 c25">print</span><span class="c2">(</span><span class="c3 c8">self</span><span class="c2">.name)</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt;</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">Every parent object we create from this code is going to have the name Ricky and brown eyes. In addition, we defined a method called </span><span class="c4">print_name()</span><span class="c3">&nbsp;to print the Parent&rsquo;s name. &nbsp;</span></p><p class="c0"><span class="c3">What if we want to create a class </span><span class="c4">Child </span><span class="c3">(representing a human child) that has a name and eye color, and can print its name, but with an extra method called </span><span class="c4">print_cartoon()</span><span class="c3">&nbsp;that prints the child&rsquo;s favorite cartoon. Furthermore, we don&rsquo;t want our Parent objects to have the </span><span class="c4">print_cartoon()</span><span class="c3">&nbsp;method, since Parent&rsquo;s don&rsquo;t watch cartoons (ok maybe they do, but in this example they don&rsquo;t). We could solve this problem with the following code:</span></p><p class="c1 c9"><span class="c4"></span></p><p class="c1"><span class="c3">&ldquo;&ldquo;&ldquo; </span></p><p class="c1"><span class="c3">https://github.com/calthoff/tstp/blob/master/part_II/more_object_oriented_programming/inheritance_ex2.py</span></p><p class="c1"><span class="c3">&nbsp;&rdquo;&rdquo;&rdquo;</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c7 c3 c5">class </span><span class="c2 c5">Parent</span><span class="c2">():</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp;</span><span class="c7 c3 c5">def </span><span class="c29 c3">__init__</span><span class="c2">(</span><span class="c3 c8">self</span><span class="c2">):</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c3 c8">self</span><span class="c2">.name = </span><span class="c12 c3">&#39;Ricky&#39;</span></p><p class="c1 c10"><span class="c12 c3">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c3 c8">self</span><span class="c2">.eye_color = </span><span class="c12 c3">&#39;brown&#39;</span></p><p class="c1 c9 c10"><span class="c12 c3"></span></p><p class="c1 c10"><span class="c12 c3">&nbsp; &nbsp;</span><span class="c7 c3 c5">def </span><span class="c2 c5">print_name</span><span class="c2">(</span><span class="c3 c8">self</span><span class="c2">):</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c3 c25">print</span><span class="c2">(</span><span class="c3 c8">self</span><span class="c2">.name)</span></p><p class="c1 c9"><span class="c4"></span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p><p class="c1 c10"><span class="c7 c3 c5">class </span><span class="c2 c5">Child</span><span class="c2">(Parent):</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp;</span><span class="c3 c25">@</span><span class="c54 c3">staticmethod</span></p><p class="c1 c10"><span class="c54 c3">&nbsp; &nbsp;</span><span class="c7 c3 c5">def </span><span class="c2 c5">print_cartoon</span><span class="c2">(favorite_cartoon):</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c3 c25">print</span><span class="c2">(favorite_cartoon)</span></p><p class="c1 c9 c10"><span class="c2"></span></p><p class="c1 c10"><span class="c2">child = Child()</span></p><p class="c1 c10"><span class="c3 c25">print</span><span class="c2">(child.eye_color)</span></p><p class="c1 c10"><span class="c2">child.print_name()</span></p><p class="c1 c10"><span class="c2">child.print_cartoon(</span><span class="c12 c3">&#39;DuckTales&#39;</span><span class="c2">)</span></p><p class="c1 c9"><span class="c4"></span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c4">&gt;&gt; brown</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; Ricky</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; DuckTale</span><span class="c4">s</span></p><p class="c1 c9"><span></span></p><p class="c1"><span class="c3">As you can see, our </span><span class="c4">Child</span><span class="c3">&nbsp;class inherited all of the functionality of our </span><span class="c4">Parent</span><span class="c3">&nbsp;class. Our child object has a name, an eye color, and the method </span><span class="c4">print_name()</span><span class="c3">, all without defining any of it in our </span><span class="c4">Child</span><span class="c3">&nbsp;class. Furthermore, we were able to add new functionality to our </span><span class="c4">Child</span><span class="c3">&nbsp;class, by defining a new method called </span><span class="c4">print_cartoon</span><span class="c3">, without affecting the </span><span class="c4">Parent</span><span class="c3">&nbsp;class. The syntax for inheriting from a parent class is to pass the name of the class you want to inherit from to your child class. So if you want your </span><span class="c4">Car</span><span class="c3">&nbsp;class to inherit from your </span><span class="c4">Manufacturer</span><span class="c3">&nbsp;class, pass your </span><span class="c4">Manufacturer</span><span class="c3">&nbsp;class into your </span><span class="c4">Car</span><span class="c3">&nbsp;Class when you define it:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# this code will not actually work as is because Manufacturer is not defined</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c7 c3 c5">class </span><span class="c2 c5">Car</span><span class="c2">(Manufacturer):</span></p><p class="c1"><span class="c2">&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp;</span><span class="c7 c3 c5">pass</span></p><h3 class="c1 c13" id="h.dn3ow6fv2grf"><span>Overriding</span></h3><p class="c0"><span class="c3">When we define a class, we can override the methods it inherits from its parent. This means we can completely change the way an inherited method works, or change only part of it.</span></p><p class="c0 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&ldquo;&ldquo;&ldquo; </span></p><p class="c1"><span class="c3">https://github.com/calthoff/tstp/blob/master/part_II/more_object_oriented_programming/overriding_ex1.py</span></p><p class="c1"><span class="c3">&nbsp;&rdquo;&rdquo;&rdquo; </span></p><p class="c0 c9"><span class="c3"></span></p><p class="c0"><span class="c7 c3 c5">class </span><span class="c2 c5">Mammal</span><span class="c2">:</span></p><p class="c0"><span class="c2">&nbsp; &nbsp;</span><span class="c7 c3 c5">def </span><span class="c29 c3">__init__</span><span class="c2">(</span><span class="c3 c8">self</span><span class="c7 c3">, </span><span class="c2">name):</span></p><p class="c0"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c3 c8">self</span><span class="c2">.hunger = </span><span class="c3 c20">100</span></p><p class="c0"><span class="c3 c20">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c3 c8">self</span><span class="c2">.tired = </span><span class="c3 c20">100</span></p><p class="c0"><span class="c3 c20">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c3 c8">self</span><span class="c2">.name = name</span></p><p class="c0 c9"><span class="c2"></span></p><p class="c0"><span class="c2">&nbsp; &nbsp;</span><span class="c7 c3 c5">def </span><span class="c2 c5">print_result</span><span class="c2">(</span><span class="c3 c8">self</span><span class="c7 c3">, </span><span class="c2">amount</span><span class="c7 c3">, </span><span class="c2">action):</span></p><p class="c0"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c3 c25">print</span><span class="c2">(</span><span class="c12 c3">&quot;{} {} decreased by {}.&quot;</span><span class="c2">.format(</span><span class="c3 c8">self</span><span class="c2">.name</span><span class="c7 c3">, </span><span class="c2">action</span><span class="c7 c3">, </span><span class="c2">amount))</span></p><p class="c0 c9"><span class="c2"></span></p><p class="c0"><span class="c2">&nbsp; &nbsp;</span><span class="c7 c3 c5">def </span><span class="c2 c5">eat</span><span class="c2">(</span><span class="c3 c8">self</span><span class="c7 c3">, </span><span class="c2">decrease):</span></p><p class="c0"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c3 c8">self</span><span class="c2">.hunger -= decrease</span></p><p class="c0"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c3 c8">self</span><span class="c2">.print_result(decrease</span><span class="c7 c3">, </span><span class="c12 c3">&#39;hunger&#39;</span><span class="c2">)</span></p><p class="c0 c9"><span class="c2"></span></p><p class="c0"><span class="c2">&nbsp; &nbsp;</span><span class="c7 c3 c5">def </span><span class="c2 c5">sleep</span><span class="c2">(</span><span class="c3 c8">self</span><span class="c7 c3">, </span><span class="c2">decrease):</span></p><p class="c0"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c3 c8">self</span><span class="c2">.tired -= decrease</span></p><p class="c0"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c3 c8">self</span><span class="c2">.print_result(decrease</span><span class="c7 c3">, </span><span class="c12 c3">&#39;tiredness&#39;</span><span class="c2">)</span></p><p class="c0 c9"><span class="c2"></span></p><p class="c0 c9"><span class="c2"></span></p><p class="c0"><span class="c7 c3 c5">class </span><span class="c2 c5">Dolphin</span><span class="c2">(Mammal):</span></p><p class="c0"><span class="c2">&nbsp; &nbsp;</span><span class="c7 c3 c5">pass</span></p><p class="c1 c9"><span class="c7 c3 c5"></span></p><p class="c0"><span class="c7 c3 c5">class </span><span class="c2 c5">Tiger</span><span class="c2">(Mammal):</span></p><p class="c0"><span class="c2">&nbsp; &nbsp;</span><span class="c7 c3 c5">def </span><span class="c2 c5">sleep</span><span class="c2">(</span><span class="c3 c8">self</span><span class="c7 c3">, </span><span class="c2">decrease):</span></p><p class="c0"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c3 c8">self</span><span class="c2">.tired -= decrease</span></p><p class="c0"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c3 c25">print</span><span class="c2">(</span><span class="c12 c3">&quot;The tiger is really tired!&quot;</span><span class="c2">)</span></p><p class="c0 c9"><span class="c2 c76"></span></p><p class="c0"><span class="c2">dolphin</span><span class="c2">&nbsp;= Dolphin(</span><span class="c12 c3">&#39;dolphin&#39;</span><span class="c2">)</span></p><p class="c0"><span class="c2">dolphin</span><span class="c2">.eat(</span><span class="c3 c20">10</span><span class="c2">)</span></p><p class="c0"><span class="c2">dolphin</span><span class="c2">.sleep(</span><span class="c3 c20">10</span><span class="c2">)</span></p><p class="c0 c9"><span class="c2"></span></p><p class="c0"><span class="c2">tiger = Tiger(</span><span class="c12 c3">&#39;tiger&#39;</span><span class="c2">)</span></p><p class="c0"><span class="c2">tiger.eat(</span><span class="c3 c20">10</span><span class="c2">)</span></p><p class="c0"><span class="c2">tiger.sleep(</span><span class="c3 c20">10</span><span class="c2">)</span></p><p class="c0"><span class="c2 c76">&nbsp; &nbsp; &nbsp; </span></p><p class="c0"><span class="c3">&gt;</span><span class="c3">&gt; dolphin hunger decreased by 10.</span></p><p class="c0"><span class="c3">&gt;&gt; dolphin tiredness decreased by 10.</span></p><p class="c0"><span class="c3">&gt;&gt; tiger hunger decreased by 10.</span></p><p class="c0"><span class="c3">&gt;&gt; The tiger is really tired!</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">In this example we created two classes that inherit from </span><span class="c4">Mammal</span><span class="c3">. The first class, </span><span class="c4">Dolphin</span><span class="c3">, simply inherits all of its functionality from the </span><span class="c4">Mammal</span><span class="c3">&nbsp;parent class and doesn&rsquo;t make any changes. The second class, however, defines it&rsquo;s own method called </span><span class="c4">sleep</span><span class="c3">, with different functionality than the parent class&rsquo;s </span><span class="c4">sleep</span><span class="c3">&nbsp;method (we change what is printed). When you define a method on a child class with the same name as an inherited method from its parent, the new method is used in place of the parent method. In this case </span><span class="c4">Tiger</span><span class="c3">&nbsp;and </span><span class="c4">Dolphin</span><span class="c3">&nbsp;have all the same functionality, except </span><span class="c4">Tiger</span><span class="c3">&nbsp;has a different sleep method, because we defined a new sleep method to override the </span><span class="c4">sleep</span><span class="c3">&nbsp;method inherited from </span><span class="c4">Tiger&rsquo;s</span><span class="c3">&nbsp;parent class, </span><span class="c4">Mammal</span><span class="c3">. &nbsp; &nbsp; </span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;The keyword </span><span class="c4">super</span><span class="c3">, let&rsquo;s us use make changes to an inherited method, while still using functionality from it. If we wanted to still use the functionality from </span><span class="c4">Mammals&rsquo; sleep </span><span class="c3">method, but just add more functionality we could do it like this:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c0"><span class="c7 c3 c5">class </span><span class="c2 c5">Tiger</span><span class="c2">(Mammal):</span></p><p class="c0"><span class="c2">&nbsp; &nbsp;</span><span class="c7 c3 c5">def </span><span class="c2 c5">sleep</span><span class="c2">(</span><span class="c3 c8">self</span><span class="c7 c3">, </span><span class="c2">decrease):</span></p><p class="c0"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c3 c25">super</span><span class="c2">().sleep(decrease)</span></p><p class="c0"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c3 c25">print</span><span class="c2">(</span><span class="c12 c3">&quot;The tiger is really tired!&quot;</span><span class="c2">)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p><p class="c0 c9"><span class="c2"></span></p><p class="c0 c9"><span class="c2"></span></p><p class="c0"><span class="c2">tiger</span><span class="c2">&nbsp;= Tiger(</span><span class="c12 c3">&#39;tiger&#39;</span><span class="c2">)</span></p><p class="c0"><span class="c2">tiger</span><span class="c2">.eat(</span><span class="c3 c20">10</span><span class="c2">)</span></p><p class="c0"><span class="c2">tiger</span><span class="c2">.sleep(</span><span class="c3 c20">10</span><span class="c2">)</span></p><p class="c0 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt;</span><span class="c3">&nbsp;tiger tiredness decreased by 10.</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; The tiger is really tired!</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">In this example we used the </span><span class="c4">super</span><span class="c3">&nbsp;keyword to call </span><span class="c4">Tiger&rsquo;s</span><span class="c3">&nbsp;</span><span class="c4">sleep</span><span class="c3">&nbsp;parent method and pass </span><span class="c4">decrease</span><span class="c3">&nbsp;to it. The parent method was executed, and then the code from </span><span class="c4">Tiger&rsquo;s</span><span class="c3">&nbsp;sleep method ran, printing &ldquo;The tiger is really tired!&rdquo;. Anytime you are in a child class&rsquo;s method, you can use </span><span class="c4">super</span><span class="c3">&nbsp;to execute a parent method. The syntax is to simply call </span><span class="c4">super()</span><span class="c3">, followed by the name of the parent method you want to call, along with any arguments you want to pass to the parent method. </span></p><p class="c0"><span class="c3">A really cool feature of Python is the ability to override the built-in methods every class automatically inherits. Every class you define in Python automatically inherits from a parent class called </span><span class="c4">Object</span><span class="c3">, and so it inherits methods like </span><span class="c4">__init__</span><span class="c3">&nbsp;which we&rsquo;ve seen, other methods like </span><span class="c4">__repr__</span><span class="c3">&nbsp;and </span><span class="c4">__add__</span><span class="c3">, along with many others. In this example, we will override these methods: &nbsp; &nbsp; &nbsp; &nbsp;</span></p><p class="c1 c9"><span class="c11 c3"></span></p><p class="c1"><span class="c3">&ldquo;&ldquo;&ldquo; </span></p><p class="c1"><span class="c3">https://github.com/calthoff/tstp/blob/master/part_II/more_object_oriented_programming/overriding_ex2.py</span></p><p class="c1"><span class="c3">&nbsp;&rdquo;&rdquo;&rdquo; </span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c10"><span class="c7 c3 c5">class </span><span class="c2 c5">StringCount</span><span class="c2">:</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp;</span><span class="c7 c3 c5">def </span><span class="c29 c3">__init__</span><span class="c2">(</span><span class="c3 c8">self</span><span class="c7 c3">, </span><span class="c2">string):</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c3 c8">self</span><span class="c2">.string = string</span></p><p class="c1 c9 c10"><span class="c2"></span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp;</span><span class="c7 c3 c5">def </span><span class="c29 c3">__repr__</span><span class="c2">(</span><span class="c3 c8">self</span><span class="c2">):</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c7 c3 c5">return </span><span class="c3 c8">self</span><span class="c2">.string</span></p><p class="c1 c9 c10"><span class="c2"></span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp;</span><span class="c7 c3 c5">def </span><span class="c29 c3">__add__</span><span class="c2">(</span><span class="c3 c8">self</span><span class="c7 c3">, </span><span class="c2">other):</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c7 c3 c5">return </span><span class="c3 c25">len</span><span class="c2">(other.string) + </span><span class="c3 c25">len</span><span class="c2">(</span><span class="c3 c8">self</span><span class="c2">.string)</span></p><p class="c1 c9 c10"><span class="c2"></span></p><p class="c1 c10"><span class="c2">s1 = </span><span class="c2 c5">StringCount</span><span class="c2">(</span><span class="c12 c3">&#39;Hello&#39;</span><span class="c2">)</span></p><p class="c1 c10"><span class="c2">s2 = </span><span class="c2 c5">StringCount</span><span class="c2">(</span><span class="c12 c3">&#39;World&#39;</span><span class="c2">)</span></p><p class="c1 c9 c10"><span class="c2"></span></p><p class="c1 c10"><span class="c3 c25">print</span><span class="c2">(s1)</span></p><p class="c1 c10"><span class="c3 c25">print</span><span class="c2">(s2)</span></p><p class="c1 c10"><span class="c3 c25">print</span><span class="c2">(s1 + s2)</span></p><p class="c1 c9 c10"><span class="c2"></span></p><p class="c1 c10"><span class="c2">&gt;&gt; &lsquo;Hello&rsquo;</span></p><p class="c1 c10"><span class="c2">&gt;&gt; &lsquo;World&rsquo;</span></p><p class="c1 c10"><span class="c2">&gt;&gt; 10</span></p><p class="c1 c9"><span class="c2"></span></p><p class="c1"><span class="c3">W</span><span class="c3">e defined a class called </span><span class="c4">StringCount</span><span class="c3">, and overrode three built-in methods: </span><span class="c4">__init__</span><span class="c3">, &nbsp;</span><span class="c4">__repr__</span><span class="c3">&nbsp;and </span><span class="c4">__add__</span><span class="c3">. We&rsquo;ve been overriding __init__ this entire chapter, so you are already familiar with overriding it. The method </span><span class="c4">__repr__</span><span class="c3">&nbsp;determines what will print when the object is passed to </span><span class="c4">print()</span><span class="c3">, and </span><span class="c4">__add__</span><span class="c3">&nbsp; determines what happens when an object is used with the addition operator. By overriding these built-in methods, we created a class, that produces an object that prints a string when passed into </span><span class="c4">print()</span><span class="c3">&mdash;but when these two objects are added together&mdash;the result is the number of characters in the first string added to the number of characters in the second string. </span><span class="c3">So StringCount(&ldquo;one&rdquo;) added to StringCount(&ldquo;two&rdquo;) produces six, because each string has three characters (the length of the string is 3) and three plus three is six</span><sup><a href="#cmnt19" id="cmnt_ref19">[s]</a></sup><sup><a href="#cmnt20" id="cmnt_ref20">[t]</a></sup><sup><a href="#cmnt21" id="cmnt_ref21">[u]</a></sup><span class="c3">. By overriding the </span><span class="c4">__add__ </span><span class="c3">method, we were able to override the behavior of the addition operator for our class. You can override of all of Python&rsquo;s built-in methods&mdash;here is a list where you can find them: </span><span class="c17 c3"><a class="c18" href="https://www.google.com/url?q=https://docs.python.org/3/reference/datamodel.html&amp;sa=D&amp;ust=1467337426575000&amp;usg=AFQjCNEkpEKhAUxF_99VeV28a69LPqYigw">https://docs.python.org/3/reference/datamodel.html</a></span><span class="c3">. &nbsp; &nbsp; </span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.n46r1qqjydcu"><span>Private variables </span></h3><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Most programming languages have the concept of private variables and methods: special variables or methods the designer of a class can create that the object has access to, but not the caller of the object. The reason being you might have a method or variable only used internally by the class, and you don&rsquo;t want the person using the class to either know about or use them. Python however, does not have private variables, and solves the problem a different way&mdash;using convention. In Python, if you have a variable or method the caller should not access, you precede the name with an underscore like this: </span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p><p class="c1"><span class="c3">&ldquo;&ldquo;&ldquo; </span></p><p class="c1"><span class="c3">https://github.com/calthoff/tstp/blob/master/part_II/more_object_oriented_programming/private_variables_ex1.py</span></p><p class="c1"><span class="c3">&nbsp;&rdquo;&rdquo;&rdquo; </span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c10"><span class="c7 c3 c5">class </span><span class="c2 c5">House</span><span class="c2">:</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp;</span><span class="c7 c3 c5">def </span><span class="c29 c3">__init__</span><span class="c2">(</span><span class="c3 c8">self</span><span class="c2">):</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c3 c61">public </span><span class="c2">= </span><span class="c12 c3">&quot;callers know they can access this&quot;</span></p><p class="c1 c10"><span class="c12 c3">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c3 c61">_private </span><span class="c2">= </span><span class="c12 c3">&quot;callers know they shouldn&#39;t access this&quot;</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">This let&rsquo;s other Python programmers know they shouldn&rsquo;t use the </span><span class="c4">_private</span><span class="c3">&nbsp;variable, and if they do, they are doing so at their own risk. For example, if this was a library we built and uploaded to pip, and other people were using this code&mdash;we would never just delete our </span><span class="c4">public</span><span class="c3">&nbsp;variable because it would break anyone&rsquo;s code that is accessing the public variable. However, we are under no obligation to keep </span><span class="c4">_private</span><span class="c3">&nbsp;around, because callers are a not supposed to be accessing i</span><span class="c3">t</span><span class="c3">, and one of the reasons they aren&rsquo;t supposed to is because we are liable to delete or change it whenever we want. </span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.tdkf6rgc3uvt"><span>Wrapping Up</span></h3><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Object-oriented programming can be tricky, and we spend an entire chapter in Part IV learning more about it. However, once you get the hang of it, you will be able to write elegant programs. </span></p><h3 class="c1 c13" id="h.x93274umj49q"><span>Challenge</span></h3><p class="c1"><span class="c3">Write as many classes as you can using inheritance to represent the animal kingdom. </span></p><p class="c1 c9"><span class="c3"></span></p><h2 class="c1 c13" id="h.ggeenz74x8l2"><span>Chapter 15. </span><span>Let&rsquo;s Read Some Code</span></h2><p class="c1 c9"><span class="c3"></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3"># https://github.com/calthoff/tstp/blob/master/part_II/lets_read_some_code/example_1.py</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c10"><span class="c7 c3 c5">class </span><span class="c2 c5">Tree</span><span class="c2">:</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp;</span><span class="c7 c3 c5">def </span><span class="c29 c3">__init__</span><span class="c2">(</span><span class="c3 c8">self</span><span class="c7 c3">, </span><span class="c2">species</span><span class="c7 c3">, </span><span class="c2">height</span><span class="c7 c3">, </span><span class="c2">width):</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c3 c8">self</span><span class="c2">.species = species</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c3 c8">self</span><span class="c2">.color = </span><span class="c12 c3">&quot;yellow&quot;</span></p><p class="c1 c10"><span class="c12 c3">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c3 c8">self</span><span class="c2">.height = height</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c3 c8">self</span><span class="c2">.width = width</span></p><p class="c1 c9 c10"><span class="c2"></span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp;</span><span class="c7 c3 c5">def </span><span class="c2 c5">change_season</span><span class="c2">(</span><span class="c3 c8">self</span><span class="c7 c3">, </span><span class="c2">season):</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp;seasons = {</span><span class="c12 c3">&#39;fall&#39;</span><span class="c2">: </span><span class="c12 c3">&#39;yellow&#39;</span><span class="c7 c3">, </span><span class="c12 c3">&#39;winter&#39;</span><span class="c2">: </span><span class="c7 c3 c5">None</span><span class="c7 c3">, </span><span class="c12 c3">&#39;spring&#39;</span><span class="c2">: </span><span class="c12 c3">&#39;green&#39;</span><span class="c7 c3">, </span><span class="c12 c3">&#39;summer&#39;</span><span class="c2">: </span><span class="c12 c3">&#39;brown&#39;</span><span class="c2">}</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c7 c3 c5">if </span><span class="c2">season </span><span class="c7 c3 c5">not in </span><span class="c2">seasons:</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c7 c3 c5">raise </span><span class="c3 c25">ValueError</span><span class="c2">(</span><span class="c12 c3">&quot;not a valid season&quot;</span><span class="c2">)</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c3 c8">self</span><span class="c2">.color = seasons[season]</span></p><p class="c1 c9 c10"><span class="c2"></span></p><p class="c1 c9 c10"><span class="c2"></span></p><p class="c1 c10"><span class="c2">pine = Tree(</span><span class="c12 c3">&#39;pine&#39;</span><span class="c7 c3">, </span><span class="c3 c20">100</span><span class="c7 c3">, </span><span class="c3 c20">5</span><span class="c2">)</span><sup><a href="#cmnt22" id="cmnt_ref22">[v]</a></sup><sup><a href="#cmnt23" id="cmnt_ref23">[w]</a></sup></p><p class="c1 c10"><span class="c2">maple = Tree(</span><span class="c12 c3">&#39;maple&#39;</span><span class="c7 c3">, </span><span class="c3 c20">200</span><span class="c7 c3">, </span><span class="c3 c20">20</span><span class="c2">)</span></p><p class="c1 c9 c10"><span class="c2"></span></p><p class="c1 c10"><span class="c3 c25">print</span><span class="c2">(pine.color)</span></p><p class="c1 c10"><span class="c3 c25">print</span><span class="c2">(maple.color)</span></p><p class="c1 c9 c10"><span class="c2"></span></p><p class="c1 c10"><span class="c2">pine.change_season(</span><span class="c12 c3">&#39;summer&#39;</span><span class="c2">)</span></p><p class="c1 c10"><span class="c2">maple.change_season(</span><span class="c12 c3">&#39;spring&#39;</span><span class="c2">)</span></p><p class="c1 c9 c10"><span class="c2"></span></p><p class="c1 c10"><span class="c3 c25">print</span><span class="c2">(pine.color)</span></p><p class="c1 c10"><span class="c3 c25">print</span><span class="c2">(maple.color)</span></p><p class="c1 c9"><span class="c2"></span></p><p class="c1 c9"><span class="c2"></span></p><p class="c1"><span class="c3"># https://github.com/calthoff/tstp/blob/master/part_II/lets_read_some_code/example_2.py</span></p><p class="c1 c9 c10"><span class="c3"></span></p><p class="c1 c10"><span class="c7 c3 c5">from </span><span class="c2">random </span><span class="c7 c3 c5">import </span><span class="c2">randint</span></p><p class="c1 c9 c10"><span class="c2"></span></p><p class="c1 c10"><span class="c7 c3 c5">class </span><span class="c2 c5">Player</span><span class="c2">:</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp;</span><span class="c7 c3 c5">def </span><span class="c29 c3">__init__</span><span class="c2">(</span><span class="c3 c8">self</span><span class="c7 c3">, </span><span class="c2">player_name):</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c3 c8">self</span><span class="c2">.name = player_name</span></p><p class="c1 c9 c10"><span class="c2"></span></p><p class="c1 c9 c10"><span class="c2"></span></p><p class="c1 c10"><span class="c7 c3 c5">class </span><span class="c2 c5">Game</span><span class="c2">:</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp;</span><span class="c7 c3 c5">def </span><span class="c29 c3">__init__</span><span class="c2">(</span><span class="c3 c8">self</span><span class="c2">):</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c3 c8">self</span><span class="c2">.score = </span><span class="c3 c20">0</span></p><p class="c1 c9 c10"><span class="c3 c20"></span></p><p class="c1 c10"><span class="c3 c20">&nbsp; &nbsp;</span><span class="c7 c3 c5">def </span><span class="c2 c5">play</span><span class="c2">(</span><span class="c3 c8">self</span><span class="c7 c3">, </span><span class="c2">player):</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp;guess = </span><span class="c7 c3 c5">None</span></p><p class="c1 c10"><span class="c7 c3 c5">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c3 c25">print</span><span class="c2">(</span><span class="c12 c3">&quot;Welcome {}&quot;</span><span class="c2">.format(player.name))</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c7 c3 c5">while </span><span class="c2">guess != </span><span class="c12 c3">&#39;q&#39;</span><span class="c2">:</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;number = randint(</span><span class="c3 c20">0</span><span class="c7 c3">, </span><span class="c3 c20">10</span><span class="c2">)</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;guess = </span><span class="c3 c25">input</span><span class="c2">(</span><span class="c12 c3">&quot;Guess a number from 0 to 10:&quot;</span><span class="c2">)</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c7 c3 c5">try</span><span class="c2">:</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;guess = </span><span class="c3 c25">int</span><span class="c2">(guess)</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c7 c3 c5">except </span><span class="c3 c25">ValueError</span><span class="c2">:</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c3 c25">print</span><span class="c2">(</span><span class="c12 c3">&#39;Please enter a number:&#39;</span><span class="c2">)</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c7 c3 c5">continue</span></p><p class="c1 c10"><span class="c7 c3 c5">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if </span><span class="c2">guess == number:</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c3 c25">print</span><span class="c2">(</span><span class="c12 c3">&#39;Great job {}. You win!&#39;</span><span class="c2">.format(player.name))</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c3 c8">self</span><span class="c2">.score += </span><span class="c3 c20">10</span></p><p class="c1 c10"><span class="c3 c20">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c3 c25">print</span><span class="c2">(</span><span class="c12 c3">&quot;The score is {}.&quot;</span><span class="c2">.format(</span><span class="c3 c8">self</span><span class="c2">.score))</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c7 c3 c5">else</span><span class="c2">:</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c3 c25">print</span><span class="c2">(</span><span class="c12 c3">&#39;Sorry {}. You lose! The number was {}&#39;</span><span class="c2">.format(player.name</span><span class="c7 c3">, </span><span class="c2">number))</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c3 c8">self</span><span class="c2">.score -= </span><span class="c3 c20">1</span></p><p class="c1 c10"><span class="c3 c20">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c3 c25">print</span><span class="c2">(</span><span class="c12 c3">&quot;The score is {}.&quot;</span><span class="c2">.format(</span><span class="c3 c8">self</span><span class="c2">.score))</span></p><p class="c1 c9 c10"><span class="c2"></span></p><p class="c1 c10"><span class="c2">game = Game()</span></p><p class="c1 c10"><span class="c2">game.play(Player(</span><span class="c12 c3">&quot;Bernie&quot;</span><span class="c2">))</span></p><p class="c1 c9"><span class="c3"></span></p><h2 class="c1 c13" id="h.qmvjb9nenvlt"><span>Chapter 16. Bringing It All Together</span></h2><h3 class="c1 c13" id="h.5sd9xegbfl34"><span>War</span></h3><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;In object-oriented programming, you create programs by designing classes representing different objects that interact with each other. In this chapter, we are going to use what we&rsquo;ve learned about classes and objects to create the card game War. If you&rsquo;ve never played, War is a game where each player draws a card from the deck, and the player with the highest card wins. </span></p><p class="c0"><span class="c3">We will start with a class that defines a card, followed by a class representing a deck, a player, and finally, a class to represent the game itself. Here is our card class: &nbsp;</span></p><p class="c0 c9"><span class="c3"></span></p><p class="c1"><span class="c3"># </span><span class="c3">https://github.com/calthoff/tstp/blob/master/part_II/war/war.py</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c10"><span class="c7 c3 c5">class </span><span class="c2 c5">Card</span><span class="c2">:</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp;</span><span class="c15 c3 c30">&quot;&quot;&quot;</span></p><p class="c1 c10"><span class="c15 c3 c30">&nbsp; &nbsp;Class representing a card in a standard 52 card deck.</span></p><p class="c1 c10"><span class="c15 c3 c30">&nbsp; &nbsp;&quot;&quot;&quot;</span></p><p class="c1 c10"><span class="c15 c3 c30">&nbsp; &nbsp;</span><span class="c2">suits = [</span><span class="c12 c3">&quot;spades&quot;</span><span class="c7 c3">, </span><span class="c3 c12">&quot;hearts&quot;</span><span class="c7 c3">, </span><span class="c12 c3">&quot;diamonds&quot;</span><span class="c7 c3">, </span><span class="c12 c3">&quot;clubs&quot;</span><span class="c2">]</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp;values = [</span><span class="c7 c3 c5">None</span><span class="c7 c3">, </span><span class="c7 c3 c5">None</span><span class="c7 c3">,</span><span class="c12 c3">&quot;2&quot;</span><span class="c7 c3">, </span><span class="c12 c3">&quot;3&quot;</span><span class="c7 c3">, </span><span class="c12 c3">&quot;4&quot;</span><span class="c7 c3">, </span><span class="c12 c3">&quot;5&quot;</span><span class="c7 c3">, </span><span class="c12 c3">&quot;6&quot;</span><span class="c7 c3">, </span><span class="c12 c3">&quot;7&quot;</span><span class="c7 c3">, </span><span class="c12 c3">&quot;8&quot;</span><span class="c7 c3">, </span><span class="c12 c3">&quot;9&quot;</span><span class="c7 c3">, </span><span class="c12 c3">&quot;10&quot;</span><span class="c7 c3">, </span><span class="c12 c3">&quot;Jack&quot;</span><span class="c7 c3">, </span><span class="c12 c3">&quot;Queen&quot;</span><span class="c7 c3">, </span><span class="c12 c3">&quot;King&quot;</span><span class="c7 c3">,</span></p><p class="c1 c10"><span class="c7 c3">&nbsp; &nbsp;</span><span class="c12 c3">&quot;Ace&quot;</span><span class="c2">]</span></p><p class="c1 c9 c10"><span class="c2"></span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp;</span><span class="c7 c3 c5">def </span><span class="c29 c3">__init__</span><span class="c2">(</span><span class="c3 c8">self</span><span class="c7 c3">, </span><span class="c2">value</span><span class="c7 c3">, </span><span class="c2">suit):</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c3 c8">self</span><span class="c2">.value = value</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c3 c8">self</span><span class="c2">.suit = suit</span></p><p class="c1 c9 c10"><span class="c2"></span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp;</span><span class="c7 c3 c5">def </span><span class="c29 c3">__lt__</span><span class="c2">(</span><span class="c3 c8">self</span><span class="c7 c3">, </span><span class="c2">other):</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c7 c3 c5">if </span><span class="c3 c8">self</span><span class="c2">.value &lt; other.value:</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c7 c3 c5">return True</span></p><p class="c1 c10"><span class="c7 c3 c5">&nbsp; &nbsp; &nbsp; &nbsp;return False</span></p><p class="c1 c9 c10"><span class="c7 c3 c5"></span></p><p class="c1 c10"><span class="c7 c3 c5">&nbsp; &nbsp;def </span><span class="c29 c3">__gt__</span><span class="c2">(</span><span class="c3 c8">self</span><span class="c7 c3">, </span><span class="c2">other):</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c7 c3 c5">if </span><span class="c3 c8">self</span><span class="c2">.value &gt; other.value:</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c7 c3 c5">return True</span></p><p class="c1 c10"><span class="c7 c3 c5">&nbsp; &nbsp; &nbsp; &nbsp;return False</span></p><p class="c1 c9 c10"><span class="c7 c3 c5"></span></p><p class="c1 c10"><span class="c7 c3 c5">&nbsp; &nbsp;def </span><span class="c29 c3">__eq__</span><span class="c2">(</span><span class="c3 c8">self</span><span class="c7 c3">, </span><span class="c2">other):</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c7 c3 c5">if </span><span class="c3 c8">self</span><span class="c2">.value == other:</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c7 c3 c5">return True</span></p><p class="c1 c9 c10"><span class="c7 c3 c5"></span></p><p class="c1 c10"><span class="c7 c3 c5">&nbsp; &nbsp;def </span><span class="c29 c3">__repr__</span><span class="c2">(</span><span class="c3 c8">self</span><span class="c2">):</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c7 c3 c5">return </span><span class="c3 c8">self</span><span class="c2">.values[</span><span class="c3 c8">self</span><span class="c2">.value] + </span><span class="c12 c3">&quot; of &quot; </span><span class="c2">+ </span><span class="c3 c8">self</span><span class="c2">.suits[</span><span class="c3 c8">self</span><span class="c2">.suit]</span></p><p class="c1 c9 c10"><span class="c7 c3 c5"></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">Note that some methods start with text in triple quotes&mdash;this is called a docstring&mdash;a string used as a comment to explain what the method does. &nbsp;Our </span><span class="c4">Card</span><span class="c3">&nbsp;class has two class variables: suits and values. </span><span class="c4">suits</span><span class="c3">&nbsp;is a list of strings representing all of the different the suits a card could be. </span><span class="c4">values</span><span class="c3">&nbsp;is a list of strings representing the different values a card could be. The first two indexes of </span><span class="c4">values</span><span class="c3">&nbsp;are </span><span class="c4">None</span><span class="c3">&nbsp;so each value matches up with its index&mdash;in other words the string &ldquo;2&rdquo; is at index 2. Our card class has two instance </span><span class="c3">variables: </span><span class="c3">suit, and number&mdash;each represented by an integer; this will represent what kind of card it is i.e., a 2 of hearts is created by setting the value of a card to 2, and the suit to 1 (the indexes corresponding to &ldquo;2&rdquo; in our </span><span class="c4">values</span><span class="c3">&nbsp;list and &ldquo;hearts&rdquo; in our </span><span class="c4">suits</span><span class="c3">&nbsp;list). </span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;In order to play War, we need to compare two cards to see which card is bigger. If they have the same value, i.e., they both are 10&rsquo;s, then the winning card is the one with the highest ranking suit (the suits in our </span><span class="c4">suits</span><span class="c3">&nbsp;list are in order from least valuable to most). We added the ability to compare two cards to our </span><span class="c4">Card</span><span class="c3">&nbsp;class by overriding three built-in comparison methods: &nbsp;</span><span class="c4">__lt___, __gt__ </span><span class="c3">and</span><span class="c4">&nbsp;__eq__</span><span class="c3">. &nbsp;These built-in method are called when you compare two objects using the comparison operators less than, greater than and equals i.e., &lt;, &gt;, =. By overriding these methods we can define what happens when our </span><span class="c4">Card</span><span class="c3">&nbsp;objects are compared&mdash;in this case </span><span class="c4">other</span><span class="c3">&nbsp;is automatically passed to each method and it represents the </span><span class="c4">Card</span><span class="c3">&nbsp;on the other side of the comparison operator. So if we do </span><span class="c4">card1 &gt; card2</span><span class="c3">, Python automatically passes in </span><span class="c4">card2</span><span class="c3">&nbsp;to each of the built-in comparison methods. In each case, we compare the value of </span><span class="c4">card1</span><span class="c3">&nbsp;to the value of </span><span class="c4">card2</span><span class="c3">&nbsp;and return </span><span class="c4">True</span><span class="c3">&nbsp;or </span><span class="c4">False</span><span class="c3">&nbsp;depending on the operator. </span></p><p class="c0"><span class="c3">We also override the built-in method </span><span class="c4">__repr__</span><span class="c3">, which we learned earlier is used to prints an object. Overriding the </span><span class="c4">__repr__</span><span class="c3">&nbsp;method lets us print a card&rsquo;s value after we&rsquo;ve created it:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c10"><span class="c2">card</span><span class="c2">&nbsp;= Card(</span><span class="c3 c20">10</span><span class="c7 c3">, </span><span class="c3 c20">2</span><span class="c2">):</span></p><p class="c1 c10"><span class="c3 c25">print </span><span class="c2">card</span></p><p class="c1 c9 c10"><span class="c4"></span></p><p class="c1 c10"><span class="c4">&gt;&gt; 10 of diamonds</span></p><p class="c1"><span class="c3">&nbsp; </span></p><p class="c1"><span class="c3">Now we need a class to represent a deck of cards:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c7 c3 c5">from </span><span class="c2">random </span><span class="c7 c3 c5">import </span><span class="c2">shuffle</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c10"><span class="c7 c3 c5">class </span><span class="c2 c5">Deck</span><span class="c2">:</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp;</span><span class="c15 c3 c30">&quot;&quot;&quot;</span></p><p class="c1 c10"><span class="c15 c3 c30">&nbsp; &nbsp;Class Representing a deck of playing cards.</span></p><p class="c1 c10"><span class="c15 c3 c30">&nbsp; &nbsp;&quot;&quot;&quot;</span></p><p class="c1 c10"><span class="c15 c3 c30">&nbsp; &nbsp;</span><span class="c7 c3 c5">def </span><span class="c29 c3">__init__</span><span class="c2">(</span><span class="c3 c8">self</span><span class="c2">):</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c3 c8">self</span><span class="c2">.cards = []</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c7 c3 c5">for </span><span class="c2">i </span><span class="c7 c3 c5">in </span><span class="c3 c25">range</span><span class="c2">(</span><span class="c3 c20">2</span><span class="c7 c3">, </span><span class="c3 c20">15</span><span class="c2">):</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c7 c3 c5">for </span><span class="c2">j </span><span class="c7 c3 c5">in </span><span class="c3 c25">range</span><span class="c2">(</span><span class="c3 c20">4</span><span class="c2">):</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c3 c8">self</span><span class="c2">.cards.append(Card(i</span><span class="c7 c3">, </span><span class="c2">j))</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp;shuffle(</span><span class="c3 c8">self</span><span class="c2">.cards)</span></p><p class="c1 c9 c10"><span class="c2"></span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp;</span><span class="c7 c3 c5">def </span><span class="c2 c5">remove_card</span><span class="c2">(</span><span class="c3 c8">self</span><span class="c2">):</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c7 c3 c5">if </span><span class="c3 c25">len</span><span class="c2">(</span><span class="c3 c8">self</span><span class="c2">.cards) == </span><span class="c3 c20">0</span><span class="c2">:</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c7 c3 c5">return</span></p><p class="c1 c10"><span class="c7 c3 c5">&nbsp; &nbsp; &nbsp; &nbsp;return </span><span class="c3 c8">self</span><span class="c2">.cards.pop()</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p><p class="c1"><span class="c3">When our object is initialized, the two for loops in </span><span class="c4">__init__</span><span class="c3">&nbsp;create all of the cards in a 52 card deck and append them to our </span><span class="c4">cards</span><span class="c3">&nbsp;list. The first loop is from 2, 15 because cards start with the value 2 and end with the value 15 (the ace). The loop will go from 2 to 15 four times because of the inner loop representing each suit. Each time around the inner loop, a new card will be created using the value from the outer loop (i.e., ace) and the suit from the inner loop (i.e. a 2 representing hearts). This results in the creation of 52 cards, one for every suit. Finally, we call the shuffle method from the random method to randomly rearrange our </span><span class="c4">cards</span><span class="c3">&nbsp;list to mimic shuffling a deck of cards. &nbsp; </span></p><p class="c0"><span class="c3">&nbsp;Our deck has just one method, </span><span class="c4">remove_card</span><span class="c3">, which returns </span><span class="c4">None</span><span class="c3">&nbsp;if our list of cards is empty, and otherwise removes and returns a card from the </span><span class="c4">cards</span><span class="c3">&nbsp;list. &nbsp;With our new </span><span class="c4">Deck</span><span class="c3">&nbsp;class, we can print out all the cards in the deck:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c10"><span class="c2">deck</span><span class="c2">&nbsp;= Deck()</span></p><p class="c1 c10"><span class="c7 c3 c5">for </span><span class="c2">card </span><span class="c7 c3 c5">in </span><span class="c2">deck</span><span class="c2">.cards:</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp;</span><span class="c3 c25">print</span><span class="c2">(card)</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p><p class="c0"><span class="c3">&gt;&gt; 4 of spades</span></p><p class="c0"><span class="c3">&gt;&gt; 8 of hearts</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">Now we need a class representing each player in the game &nbsp;so we can keep track of each player&rsquo;s cards, as well as each round they win. </span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c10"><span class="c7 c3 c5">class </span><span class="c2 c5">Player</span><span class="c2">:</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp;</span><span class="c15 c3 c30">&quot;&quot;&quot;</span></p><p class="c1 c10"><span class="c15 c3 c30">&nbsp; &nbsp;Class representing a player in our game.</span></p><p class="c1 c10"><span class="c15 c3 c30">&nbsp; &nbsp;&quot;&quot;&quot;</span></p><p class="c1 c10"><span class="c15 c3 c30">&nbsp; &nbsp;</span><span class="c7 c3 c5">def </span><span class="c29 c3">__init__</span><span class="c2">(</span><span class="c3 c8">self</span><span class="c7 c3">, </span><span class="c2">name):</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c3 c8">self</span><span class="c2">.wins = </span><span class="c3 c20">0</span></p><p class="c1 c10"><span class="c3 c20">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c3 c8">self</span><span class="c2">.card = </span><span class="c7 c3 c5">None</span></p><p class="c1 c10"><span class="c7 c3 c5">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c3 c8">self</span><span class="c2">.name = name</span></p><p class="c1 c9"><span class="c4"></span></p><p class="c1"><span class="c3">Our </span><span class="c4">Player</span><span class="c3">&nbsp;class has three instance variables: </span><span class="c4">wins</span><span class="c3">&nbsp;to keep track of game wins, </span><span class="c4">card</span><span class="c3">&nbsp;to represent the current card the player is holding, and </span><span class="c4">name</span><span class="c3">&nbsp;to keep track of the player&rsquo;s name. </span></p><p class="c1 c9"><span class="c4"></span></p><p class="c1"><span class="c3">Finally, we need to create the class representing the game itself:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c10"><span class="c7 c3 c5">class </span><span class="c2 c5">Game</span><span class="c2">:</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp;</span><span class="c15 c3 c30">&quot;&quot;&quot;</span></p><p class="c1 c10"><span class="c15 c3 c30">&nbsp; &nbsp;Class representing the game of War.</span></p><p class="c1 c10"><span class="c15 c3 c30">&nbsp; &nbsp;&quot;&quot;&quot;</span></p><p class="c1 c10"><span class="c15 c3 c30">&nbsp; &nbsp;</span><span class="c7 c3 c5">def </span><span class="c29 c3">__init__</span><span class="c2">(</span><span class="c3 c8">self</span><span class="c2">):</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp;name1 = </span><span class="c3 c25">input</span><span class="c2">(</span><span class="c12 c3">&quot;player1 name &quot;</span><span class="c2">)</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp;name2 = </span><span class="c3 c25">input</span><span class="c2">(</span><span class="c12 c3">&quot;player2 name &quot;</span><span class="c2">)</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c3 c8">self</span><span class="c2">.deck = Deck()</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c3 c8">self</span><span class="c2">.player1 = Player(name1)</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c3 c8">self</span><span class="c2">.player2 = Player(name2)</span></p><p class="c1 c9 c10"><span class="c2"></span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp;</span><span class="c7 c3 c5">def </span><span class="c2 c5">play_game</span><span class="c2">(</span><span class="c3 c8">self</span><span class="c2">):</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp;cards = </span><span class="c3 c8">self</span><span class="c2">.deck.cards</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c3 c25">print</span><span class="c2">(</span><span class="c12 c3">&quot;beginning War!&quot;</span><span class="c2">)</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c7 c3 c5">while </span><span class="c3 c25">len</span><span class="c2">(cards) &gt;= </span><span class="c3 c20">2</span><span class="c2">:</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;player1_card = </span><span class="c3 c8">self</span><span class="c2">.deck.remove_card()</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;player2_card = </span><span class="c3 c8">self</span><span class="c2">.deck.remove_card()</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c3 c25">print</span><span class="c2">(</span><span class="c12 c3">&quot;{} drew {} {} drew {}&quot;</span><span class="c2">.format(</span><span class="c3 c8">self</span><span class="c2">.player1.name</span><span class="c7 c3">, </span><span class="c2">player1_card</span><span class="c7 c3">, </span></p><p class="c1 c10"><span class="c3 c8">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;self</span><span class="c2">.player2.name</span><span class="c7 c3">, </span><span class="c2">player2_card))</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c7 c3 c5">if </span><span class="c2">player1_card &gt; player2_card:</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c3 c8">self</span><span class="c2">.player1.wins += </span><span class="c3 c20">1</span></p><p class="c1 c10"><span class="c3 c20">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c3 c25">print</span><span class="c2">(</span><span class="c12 c3">&quot;{} wins this round&quot;</span><span class="c2">.format(</span><span class="c3 c8">self</span><span class="c2">.player1.name))</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c7 c3 c5">else</span><span class="c2">:</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c3 c8">self</span><span class="c2">.player2.wins += </span><span class="c3 c20">1</span></p><p class="c1 c10"><span class="c3 c20">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c3 c25">print</span><span class="c2">(</span><span class="c12 c3">&quot;{} wins this round&quot;</span><span class="c2">.format(</span><span class="c3 c8">self</span><span class="c2">.player2.name))</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c3 c25">print</span><span class="c2">(</span><span class="c12 c3">&quot;The War is over.{} wins&quot;</span><span class="c2">.format(</span><span class="c3 c8">self</span><span class="c2">.winner(</span><span class="c3 c8">self</span><span class="c2">.player1</span><span class="c7 c3">, </span><span class="c3 c8">self</span><span class="c2">.player2)))</span></p><p class="c1 c9 c10"><span class="c2"></span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp;</span><span class="c3 c25">@</span><span class="c54 c3">staticmethod</span></p><p class="c1 c10"><span class="c54 c3">&nbsp; &nbsp;</span><span class="c7 c3 c5">def </span><span class="c2 c5">winner</span><span class="c2">(player1</span><span class="c7 c3">, </span><span class="c2">player2):</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c7 c3 c5">if </span><span class="c2">player1.wins &gt; player2.wins:</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c7 c3 c5">return </span><span class="c2">player1.name</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c7 c3 c5">if </span><span class="c2">player1.wins &lt; player2.wins:</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c7 c3 c5">return </span><span class="c2">player2.name</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c7 c3 c5">return </span><span class="c12 c3">&quot;It was a tie!&quot;</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">When our game object is initialized, we use </span><span class="c4">input</span><span class="c3">&nbsp;to collect the names of the two players in the game in our </span><span class="c4">__init__</span><span class="c3">&nbsp;method. We then create a new </span><span class="c4">Deck</span><span class="c3">&nbsp;object and store it in the instance variable deck, as well as two </span><span class="c4">Player</span><span class="c3">&nbsp;objects using the names we collected, stored in the instance variables </span><span class="c4">player1</span><span class="c3">&nbsp;and </span><span class="c4">player2</span><span class="c3">. </span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Our </span><span class="c4">Game</span><span class="c3">&nbsp;class has a method </span><span class="c4">play_game</span><span class="c3">&nbsp;where our main game loop is defined. When </span><span class="c4">play_game</span><span class="c3">&nbsp;is called, it loops through the cards, drawing two cards in each loop until there are none left. One card is assigned to </span><span class="c4">player1</span><span class="c3">&nbsp;and the other card is assigned to </span><span class="c4">player2</span><span class="c3">. Since we overrode the comparison operators for our </span><span class="c4">Card</span><span class="c3">&nbsp;class, we can simply compare the two cards to see which card is greater. Every time a player wins, we increment the </span><span class="c4">wins</span><span class="c3">&nbsp;instance variable for that player, and print a message about which player won. Finally, when we run out of cards, we call the winner method, passing in both </span><span class="c4">player1</span><span class="c3">&nbsp;and </span><span class="c4">player2</span><span class="c3">, and it compares the two </span><span class="c4">Player</span><span class="c3">&nbsp;objects to see who has more wins, printing out either the name of the winner or that it was a tie. The winner method has </span><span class="c4">@staticmethod</span><span class="c3">&nbsp;over it. This let&rsquo;s Python know the method should not get passed </span><span class="c4">self </span><span class="c3">(why pass a method self if it&rsquo;s not going to use it?). Here is our complete code:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c7 c3 c5">from </span><span class="c2">random </span><span class="c7 c3 c5">import </span><span class="c2">shuffle</span><sup><a href="#cmnt24" id="cmnt_ref24">[x]</a></sup><sup><a href="#cmnt25" id="cmnt_ref25">[y]</a></sup></p><p class="c1 c9"><span class="c2 c76"></span></p><p class="c1 c10"><span class="c7 c3 c5">class </span><span class="c2 c5">Card</span><span class="c2">:</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp;</span><span class="c15 c3 c30">&quot;&quot;&quot;</span></p><p class="c1 c10"><span class="c15 c3 c30">&nbsp; &nbsp;Class representing a card in a standard 52 card deck.</span></p><p class="c1 c10"><span class="c15 c3 c30">&nbsp; &nbsp;&quot;&quot;&quot;</span></p><p class="c1 c10"><span class="c15 c3 c30">&nbsp; &nbsp;</span><span class="c2">suits = [</span><span class="c12 c3">&quot;spades&quot;</span><span class="c7 c3">, </span><span class="c12 c3">&quot;hearts&quot;</span><span class="c7 c3">, </span><span class="c12 c3">&quot;diamonds&quot;</span><span class="c7 c3">, </span><span class="c12 c3">&quot;clubs&quot;</span><span class="c2">]</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp;values = [</span><span class="c7 c3 c5">None</span><span class="c7 c3">, </span><span class="c7 c3 c5">None</span><span class="c7 c3">,</span><span class="c12 c3">&quot;2&quot;</span><span class="c7 c3">, </span><span class="c12 c3">&quot;3&quot;</span><span class="c7 c3">, </span><span class="c12 c3">&quot;4&quot;</span><span class="c7 c3">, </span><span class="c12 c3">&quot;5&quot;</span><span class="c7 c3">, </span><span class="c12 c3">&quot;6&quot;</span><span class="c7 c3">, </span><span class="c12 c3">&quot;7&quot;</span><span class="c7 c3">, </span><span class="c12 c3">&quot;8&quot;</span><span class="c7 c3">, </span><span class="c12 c3">&quot;9&quot;</span><span class="c7 c3">, </span><span class="c12 c3">&quot;10&quot;</span><span class="c7 c3">, </span><span class="c12 c3">&quot;Jack&quot;</span><span class="c7 c3">, </span><span class="c12 c3">&quot;Queen&quot;</span><span class="c7 c3">, </span><span class="c12 c3">&quot;King&quot;</span><span class="c7 c3">,</span></p><p class="c1 c10"><span class="c7 c3">&nbsp; &nbsp;</span><span class="c12 c3">&quot;Ace&quot;</span><span class="c2">]</span></p><p class="c1 c9 c10"><span class="c2"></span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp;</span><span class="c7 c3 c5">def </span><span class="c29 c3">__init__</span><span class="c2">(</span><span class="c3 c8">self</span><span class="c7 c3">, </span><span class="c2">value</span><span class="c7 c3">, </span><span class="c2">suit):</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c3 c8">self</span><span class="c2">.value = value</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c3 c8">self</span><span class="c2">.suit = suit</span></p><p class="c1 c9 c10"><span class="c2"></span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp;</span><span class="c7 c3 c5">def </span><span class="c29 c3">__lt__</span><span class="c2">(</span><span class="c3 c8">self</span><span class="c7 c3">, </span><span class="c2">other):</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c7 c3 c5">if </span><span class="c3 c8">self</span><span class="c2">.value &lt; other.value:</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c7 c3 c5">return True</span></p><p class="c1 c10"><span class="c7 c3 c5">&nbsp; &nbsp; &nbsp; &nbsp;return False</span></p><p class="c1 c9 c10"><span class="c7 c3 c5"></span></p><p class="c1 c10"><span class="c7 c3 c5">&nbsp; &nbsp;def </span><span class="c29 c3">__gt__</span><span class="c2">(</span><span class="c3 c8">self</span><span class="c7 c3">, </span><span class="c2">other):</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c7 c3 c5">if </span><span class="c3 c8">self</span><span class="c2">.value &gt; other.value:</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c7 c3 c5">return True</span></p><p class="c1 c10"><span class="c7 c3 c5">&nbsp; &nbsp; &nbsp; &nbsp;return False</span></p><p class="c1 c9 c10"><span class="c7 c3 c5"></span></p><p class="c1 c10"><span class="c7 c3 c5">&nbsp; &nbsp;def </span><span class="c29 c3">__eq__</span><span class="c2">(</span><span class="c3 c8">self</span><span class="c7 c3">, </span><span class="c2">other):</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c7 c3 c5">if </span><span class="c3 c8">self</span><span class="c2">.value == other:</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c7 c3 c5">return True</span></p><p class="c1 c9 c10"><span class="c7 c3 c5"></span></p><p class="c1 c10"><span class="c7 c3 c5">&nbsp; &nbsp;def </span><span class="c29 c3">__repr__</span><span class="c2">(</span><span class="c3 c8">self</span><span class="c2">):</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c7 c3 c5">return </span><span class="c3 c8">self</span><span class="c2">.values[</span><span class="c3 c8">self</span><span class="c2">.value] + </span><span class="c12 c3">&quot; of &quot; </span><span class="c2">+ </span><span class="c3 c8">self</span><span class="c2">.suits[</span><span class="c3 c8">self</span><span class="c2">.suit]</span></p><p class="c1 c9 c10"><span class="c2"></span></p><p class="c1 c9 c10"><span class="c2"></span></p><p class="c1 c10"><span class="c7 c3 c5">class </span><span class="c2 c5">Deck</span><span class="c2">:</span></p><p class="c1 c10"><span class="c2">&nbsp; </span><span class="c15 c3 c30">&quot;&quot;&quot;</span></p><p class="c1 c10"><span class="c15 c3 c30">&nbsp; Class Representing a deck of playing cards.</span></p><p class="c1 c10"><span class="c15 c3 c30">&nbsp; &quot;&quot;&quot;</span></p><p class="c1 c10"><span class="c15 c3 c30">&nbsp; </span><span class="c7 c3 c5">def </span><span class="c29 c3">__init__</span><span class="c2">(</span><span class="c3 c8">self</span><span class="c2">):</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; </span><span class="c3 c8">self</span><span class="c2">.cards = []</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; </span><span class="c7 c3 c5">for </span><span class="c2">i </span><span class="c7 c3 c5">in </span><span class="c3 c25">range</span><span class="c2">(</span><span class="c3 c20">2</span><span class="c7 c3">, </span><span class="c3 c20">15</span><span class="c2">):</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span class="c7 c3 c5">for </span><span class="c2">j </span><span class="c7 c3 c5">in </span><span class="c3 c25">range</span><span class="c2">(</span><span class="c3 c20">4</span><span class="c2">):</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span class="c3 c8">self</span><span class="c2">.cards.append(Card(i</span><span class="c7 c3">, </span><span class="c2">j))</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; shuffle(</span><span class="c3 c8">self</span><span class="c2">.cards)</span></p><p class="c1 c9 c10"><span class="c2"></span></p><p class="c1 c10"><span class="c2">&nbsp; </span><span class="c7 c3 c5">def </span><span class="c2 c5">remove_card</span><span class="c2">(</span><span class="c3 c8">self</span><span class="c2">):</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; </span><span class="c7 c3 c5">if </span><span class="c3 c25">len</span><span class="c2">(</span><span class="c3 c8">self</span><span class="c2">.cards) == </span><span class="c3 c20">0</span><span class="c2">:</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span class="c7 c3 c5">return</span></p><p class="c1 c10"><span class="c7 c3 c5">&nbsp; &nbsp; &nbsp; return </span><span class="c3 c8">self</span><span class="c2">.cards.pop()</span></p><p class="c1 c9 c10"><span class="c2"></span></p><p class="c1 c9 c10"><span class="c2"></span></p><p class="c1 c10"><span class="c7 c3 c5">class </span><span class="c2 c5">Player</span><span class="c2">:</span></p><p class="c1 c10"><span class="c2">&nbsp; </span><span class="c15 c3 c30">&quot;&quot;&quot;</span></p><p class="c1 c10"><span class="c15 c3 c30">&nbsp; Class representing a player in our game.</span></p><p class="c1 c10"><span class="c15 c3 c30">&nbsp; &quot;&quot;&quot;</span></p><p class="c1 c10"><span class="c15 c3 c30">&nbsp; </span><span class="c7 c3 c5">def </span><span class="c3 c29">__init__</span><span class="c2">(</span><span class="c3 c8">self</span><span class="c7 c3">, </span><span class="c2">name):</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; </span><span class="c3 c8">self</span><span class="c2">.wins = </span><span class="c3 c20">0</span></p><p class="c1 c10"><span class="c3 c20">&nbsp; &nbsp; &nbsp; </span><span class="c3 c8">self</span><span class="c2">.card = </span><span class="c7 c3 c5">None</span></p><p class="c1 c10"><span class="c7 c3 c5">&nbsp; &nbsp; &nbsp; </span><span class="c3 c8">self</span><span class="c2">.name = name</span></p><p class="c1 c9 c10"><span class="c2"></span></p><p class="c1 c9 c10"><span class="c2"></span></p><p class="c1 c10"><span class="c7 c3 c5">class </span><span class="c2 c5">Game</span><span class="c2">:</span></p><p class="c1 c10"><span class="c2">&nbsp; </span><span class="c15 c3 c30">&quot;&quot;&quot;</span></p><p class="c1 c10"><span class="c15 c3 c30">&nbsp; Class representing the game of War.</span></p><p class="c1 c10"><span class="c15 c3 c30">&nbsp; &quot;&quot;&quot;</span></p><p class="c1 c10"><span class="c15 c3 c30">&nbsp; </span><span class="c7 c3 c5">def </span><span class="c29 c3">__init__</span><span class="c2">(</span><span class="c3 c8">self</span><span class="c2">):</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; name1 = </span><span class="c3 c25">input</span><span class="c2">(</span><span class="c12 c3">&quot;player1 name &quot;</span><span class="c2">)</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; name2 = </span><span class="c3 c25">input</span><span class="c2">(</span><span class="c12 c3">&quot;player2 name &quot;</span><span class="c2">)</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; </span><span class="c3 c8">self</span><span class="c2">.deck = Deck()</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; </span><span class="c3 c8">self</span><span class="c2">.player1 = Player(name1)</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; </span><span class="c3 c8">self</span><span class="c2">.player2 = Player(name2)</span></p><p class="c1 c9 c10"><span class="c2"></span></p><p class="c1 c10"><span class="c2">&nbsp; </span><span class="c7 c3 c5">def </span><span class="c2 c5">play_game</span><span class="c2">(</span><span class="c3 c8">self</span><span class="c2">):</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; cards = </span><span class="c3 c8">self</span><span class="c2">.deck.cards</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; </span><span class="c3 c25">print</span><span class="c2">(</span><span class="c12 c3">&quot;beginning War!&quot;</span><span class="c2">)</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; user_input = </span><span class="c7 c3 c5">None</span></p><p class="c1 c10"><span class="c7 c3 c5">&nbsp; &nbsp; &nbsp; while </span><span class="c3 c25">len</span><span class="c2">(cards) &gt;= </span><span class="c3 c20">2 </span><span class="c7 c3 c5">and </span><span class="c2">user_input != </span><span class="c12 c3">&#39;q&#39;</span><span class="c2">:</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; user_input = </span><span class="c3 c25">input</span><span class="c2">(</span><span class="c12 c3">&quot;press d to draw cards q to quit&quot;</span><span class="c2">)</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; player1_card = </span><span class="c3 c8">self</span><span class="c2">.deck.remove_card()</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; player2_card = </span><span class="c3 c8">self</span><span class="c2">.deck.remove_card()</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span class="c3 c25">print</span><span class="c2">(</span><span class="c12 c3">&quot;{} drew {} {} drew {}&quot;</span><span class="c2">.format(</span><span class="c3 c8">self</span><span class="c2">.player1.name</span><span class="c7 c3">, </span><span class="c2">player1_card</span><span class="c7 c3">,</span></p><p class="c1 c10"><span class="c7 c3">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span class="c3 c8">self</span><span class="c2">.player2.name</span><span class="c7 c3">, </span><span class="c2">player2_card))</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span class="c7 c3 c5">if </span><span class="c2">player1_card &gt; player2_card:</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span class="c3 c8">self</span><span class="c2">.player1.wins += </span><span class="c3 c20">1</span></p><p class="c1 c10"><span class="c3 c20">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span class="c3 c25">print</span><span class="c2">(</span><span class="c12 c3">&quot;{} wins this round&quot;</span><span class="c2">.format(</span><span class="c3 c8">self</span><span class="c2">.player1.name))</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span class="c7 c3 c5">else</span><span class="c2">:</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span class="c3 c8">self</span><span class="c2">.player2.wins += </span><span class="c3 c20">1</span></p><p class="c1 c10"><span class="c3 c20">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span class="c3 c25">print</span><span class="c2">(</span><span class="c12 c3">&quot;{} wins this round&quot;</span><span class="c2">.format(</span><span class="c3 c8">self</span><span class="c2">.player2.name))</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; </span><span class="c3 c25">print</span><span class="c2">(</span><span class="c12 c3">&quot;The War is over.{} wins&quot;</span><span class="c2">.format(</span><span class="c3 c8">self</span><span class="c2">.winner(</span><span class="c3 c8">self</span><span class="c2">.player1</span><span class="c7 c3">, </span><span class="c3 c8">self</span><span class="c2">.player2)))</span></p><p class="c1 c9 c10"><span class="c2"></span></p><p class="c1 c10"><span class="c2">&nbsp; </span><span class="c3 c25">@</span><span class="c54 c3">staticmethod</span></p><p class="c1 c10"><span class="c54 c3">&nbsp; </span><span class="c7 c3 c5">def </span><span class="c2 c5">winner</span><span class="c2">(player1</span><span class="c7 c3">, </span><span class="c2">player2):</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; </span><span class="c7 c3 c5">if </span><span class="c2">player1.wins &gt; player2.wins:</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span class="c7 c3 c5">return </span><span class="c2">player1.name</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; </span><span class="c7 c3 c5">if </span><span class="c2">player1.wins &lt; player2.wins:</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span class="c7 c3 c5">return </span><span class="c2">player2.name</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; </span><span class="c7 c3 c5">return </span><span class="c12 c3">&quot;It was a tie!&quot;</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">We can play our new game with:</span><sup><a href="#cmnt26" id="cmnt_ref26">[z]</a></sup></p><p class="c1 c9 c10"><span class="c3"></span></p><p class="c1 c10"><span class="c2">game</span><span class="c2">&nbsp;= Game()</span></p><p class="c1 c10"><span class="c2">game</span><span class="c2">.play_game()</span></p><p class="c1 c9 c10"><span class="c4 c28 c76"></span></p><p class="c1 c10"><span class="c4">&gt;&gt; </span><span class="c4">&quot;player1 name &quot;</span></p><p class="c1 c10"><span class="c4">...</span></p><h2 class="c1 c13" id="h.n6yu3d4nzybt"><span>Chapter 17.</span><span>Practice</span></h2><h3 class="c1 c13" id="h.g67uwk2urwzh"><span>Exercises</span></h3><ol class="c45 lst-kix_gm333outdumx-0 start" start="1"><li class="c1 c10 c38"><span>Build an object-oriented text-based version of Blackjack.</span></li><li class="c1 c10 c38"><span>Build a web scraper to collect data from another website.</span></li><li class="c1 c10 c38"><span>Find a Python project you are interested in hosted on pip,(hint look on GitHub), download it, and use it in a program.</span></li></ol><p class="c1 c9"><span></span></p><h3 class="c1 c13" id="h.jnh4tpoqy5ix"><span>Read</span></h3><p class="c1 c9"><span class="c11"></span></p><ol class="c45 lst-kix_gbfbgvqhgeod-0 start" start="1"><li class="c1 c10 c38"><span class="c17 c11"><a class="c18" href="https://www.google.com/url?q=https://julien.danjou.info/blog/2013/guide-python-static-class-abstract-methods&amp;sa=D&amp;ust=1467337426748000&amp;usg=AFQjCNHe1-MI_IjlGt9_GOovKVzqP6gETg">https://julien.danjou.info/blog/2013/guide-python-static-class-abstract-methods</a></span></li><li class="c1 c10 c38"><span class="c17 c11"><a class="c18" href="https://www.google.com/url?q=http://stackoverflow.com/questions/2573135/python-progression-path-from-apprentice-to-guru&amp;sa=D&amp;ust=1467337426749000&amp;usg=AFQjCNG8DE4nElESZHcn3K-T1uccN_hI9A">http://stackoverflow.com/questions/2573135/python-progression-path-from-apprentice-to-guru</a></span><sup><a href="#cmnt27" id="cmnt_ref27">[aa]</a></sup></li></ol><p class="c1 c9"><span class="c11"></span></p><a id="t.ed45be7d6bbf8e58bdf3ed71e5361a6597bafb62"></a><a id="t.1"></a><table class="c31"><tbody><tr class="c65"><td class="c109" colspan="1" rowspan="1"><p class="c86 c1 c9"><span class="c68 c33 c22"></span></p></td><td class="c85" colspan="1" rowspan="1"><p class="c86 c1 c9"><span class="c68 c33 c22"></span></p></td></tr><tr class="c65"><td class="c100" colspan="1" rowspan="1"><p class="c1 c90 c9 c104"><span class="c68 c33 c22"></span></p></td><td class="c64" colspan="1" rowspan="1"><p class="c86 c1 c9"><span class="c68 c33 c22"></span></p></td></tr><tr class="c65"><td class="c100" colspan="1" rowspan="1"><p class="c104 c1 c90 c9"><span class="c68 c33 c22"></span></p></td><td class="c64" colspan="1" rowspan="1"><p class="c1 c9 c86"><span class="c33 c22 c68"></span></p></td></tr></tbody></table><p class="c1 c9"><span class="c11"></span></p><h1 class="c1 c13" id="h.cthc5foiynuf"><span>Part III</span></h1><h1 class="c1 c13" id="h.mkbi036p4c76"><span>Learn to use Tools to Enhance your Performance</span></h1><p class="c1 c9"><span></span></p><h2 class="c1 c13" id="h.9djb3u12svy"><span>Reading Documentation</span></h2><p class="c1"><span class="c3">&ldquo;What I cannot build, I do not understand.&rdquo;</span></p><p class="c1"><span class="c3">&mdash; Richard Feynman</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;As a software engineer, you will spend a lot of time reading. Not just books, but documentation as well. Companies often refer to the &ldquo;stack&rdquo; they use. This refers to all of the different technologies they use to build their product. One such example is the MEAN stack, which stands for Mongo, Express, Angular and Node. Each one is a different technology. For example, Mongo is a database whereas Express is a web framework. If you work for a company that uses the MEAN stack, you need to learn each of these technologies, and that means reading documentation. Documentation is text, usually located online or in a readme file, explaining how to use a piece of software. If you&rsquo;ve never seen programming documentation, you can find Python&rsquo;s documentation here: &nbsp;https://docs.python.org/3/. </span></p><p class="c0"><span class="c3">The problem with programming documentation is it assumes the readers are experienced programmers; it&rsquo;s almost as if there is a secret document out there everyone is basing their documentation on. It turns out there is. Many of the conventions for writing documentation are over 30 years old, originating from the Unix man page synopsis format.</span><span class="c41 c3">9 </span><span class="c3">&nbsp;In this chapter, we will go over the conventions originating from it. We will also cover some of the programming conventions used in the Python programming language. &nbsp;The conventions explained in this chapter will also be used throughout the book. </span></p><h3 class="c1 c13" id="h.33vla5i5gpfz"><span>Brackets [ ]</span></h3><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;When you are reading documentation, you will often see a word surrounded by brackets i.e., </span><span class="c4">[hello!]</span><span class="c3">. Here is an example from Python&rsquo;s documentation:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c0"><span class="c4">os.mkdir(path[, mode])</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">This means there is a function or method in the os module called </span><span class="c4">mkdir</span><span class="c3">. </span><span class="c4">mkdir</span><span class="c3">&nbsp;takes two parameters, </span><span class="c4">path</span><span class="c3">&nbsp;and </span><span class="c4">mode</span><span class="c3">. </span><span class="c4">path</span><span class="c3">&nbsp;is a required parameter, it has to be passed into the function, whereas </span><span class="c4">mode</span><span class="c3">&nbsp;is also a parameter, but it is optional (because it is surrounded by brackets). This is specific to Python&rsquo;s documentation. Generally, whenever there are brackets in documentation, it means you need to replace whatever is in the brackets when you replicate the example, and the word inside the brackets is a hint for what you need to replace the brackets with. &nbsp;</span></p><h3 class="c1 c13" id="h.af081k72e7s5"><span>Ellipses ...</span></h3><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Ellipses mean the previous argument may be repeated. The following code from MySQL&rsquo;s documentation shows you how to update a column in a database table. </span></p><p class="c1 c9"><span class="c3"></span></p><p class="c0"><span class="c4">UPDATE &nbsp; &nbsp;</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[LOW_PRIORITY] [IGNORE] </span><span class="c15 c4">table_reference</span></p><p class="c1"><span class="c4">&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SET </span><span class="c15 c4">col_name1</span><span class="c4">={</span><span class="c15 c4">expr1</span><span class="c4">|DEFAULT} [, </span><span class="c15 c4">col_name2</span><span class="c4">={</span><span class="c15 c4">expr2</span><span class="c4">|DEFAULT}] ...</span></p><p class="c1"><span class="c4">&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[WHERE </span><span class="c15 c4">where_condition</span><span class="c4">]</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">The three periods mean you can repeat the argument on the second line in the brackets as many times as you want. This MySQL code changes a column based on conditions set in the third line, so the ellipses means that by repeating line 2, you can change more than one column.</span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.oxpygbb8fv0y"><span>pipes |</span></h3><p class="c1"><span class="c3">The pipe symbol &ldquo;|&rdquo; is used to represent choices. In the second line of the previous example, </span><span class="c15 c3">expr1 | DEFAULT</span><span class="c3">&nbsp;means you can set the column to either </span><span class="c15 c3">expr1</span><span class="c3">&nbsp;or to </span><span class="c4">DEFAULT</span><span class="c3">. </span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.jraohdkniwux"><span>Flags -F</span></h3><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;The </span><span class="c4">-</span><span class="c3">&nbsp;and </span><span class="c4">--</span><span class="c3">&nbsp;symbols represent a flag. Flags are used are to add options to programs when you run them from the command line. The command line is the program you use to communicate with your operating system, which we cover in the chapter The Command Line. A flag represents true or false for an option when giving a command from the command line. Here is partial documentation for the linux command </span><span class="c4">ls</span><span class="c3">&nbsp;</span><span class="c41 c3">2</span><span class="c3">:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3 c5">-a</span><span class="c3">, </span><span class="c3 c5">--all</span></p><p class="c1"><span class="c3">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; do not ignore entries starting with .</span></p><p class="c1"><span class="c3">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c3 c5">--author</span></p><p class="c1"><span class="c3">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; with </span><span class="c3 c5">-l</span><span class="c3">, print the author of each file</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c4">ls</span><span class="c3">&nbsp;is a command used for listing directories from the command line. This documentation means you can add the flags </span><span class="c4">-a</span><span class="c3">, and </span><span class="c4">--author</span><span class="c3">&nbsp;to the command </span><span class="c4">ls</span><span class="c3">, so both </span><span class="c4">ls -a</span><span class="c3">&nbsp;and </span><span class="c4">ls --author </span><span class="c3">are valid commands. You can also combine commands, so </span><span class="c4">ls -a --author</span><span class="c3">&nbsp;will work as well. Please note this is example is for Linux, on Unix you can use all the same commands but with one dash instead of two. If you&rsquo;ve never used the command line, don&rsquo;t worry, this will &nbsp;make more sense after chapter The Command Line, and you may want to come back later and revisit this section. </span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.o4sc50cju1t"><span>Dollar Sign $</span></h3><p class="c0"><span class="c3">Working on projects in the past, I kept seeing instructions prefaced with </span><span class="c4">$</span><span class="c3">, and had no idea what to do. Whenever you are following programming instructions, and see </span><span class="c4">$</span><span class="c3">, it means it is a command that you should type into the command line. </span></p><h3 class="c1 c13" id="h.4lsii529u2nk"><span>Programming Conventions </span></h3><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Programming languages have conventions as well. Conventions are rules generally followed by the community using the language. For example, in Python, function names are written </span><span class="c4">like_this</span><span class="c3">&nbsp;whereas class names are written </span><span class="c4">LikeThis</span><span class="c3">. There is nothing stopping you from making a class called</span><span class="c4">&nbsp;like_this</span><span class="c3">, except it might annoy the people who end up reading your code. Python has a guide outlining its conventions you can read at: https://www.python.org/dev/peps/pep-0008/.</span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.ks5z1jxxq7xd"><span>Wrapping Up</span></h3><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;When you are working with something new for the first time, whether it&rsquo;s the unofficial language of writing documentation, or a new programming language, it&#39;s important to realize each language most likely comes with its own conventions. Learning these conventions will help you fully understand what you are reading. </span></p><h3 class="c1 c13" id="h.v5gdcefcys2c"><span>Documentation Challenge</span></h3><p class="c1"><span class="c3">Try practicing your skills reading documentation by going to</span><span class="c3"><a class="c18" href="https://www.google.com/url?q=http://dev.mysql.com/doc/refman/5.7/en/create-table.html&amp;sa=D&amp;ust=1467337426776000&amp;usg=AFQjCNHc9_slQ-XaiZHIhoUPb7Q05gw-pA">&nbsp;</a></span><span class="c17 c3"><a class="c18" href="https://www.google.com/url?q=http://dev.mysql.com/doc/refman/5.7/en/create-table.html&amp;sa=D&amp;ust=1467337426776000&amp;usg=AFQjCNHc9_slQ-XaiZHIhoUPb7Q05gw-pA">http://dev.mysql.com/doc/refman/5.7/en/create-table.html</a></span><span class="c3">&nbsp;and reading through the MySQL documentation to create a new table. See if you can get a general idea of the different options available. </span></p><p class="c1 c9"><span></span></p><h2 class="c1 c13" id="h.o4t08gv1tqzt"><span>Virtual Machines</span><sup><a href="#cmnt28" id="cmnt_ref28">[ab]</a></sup></h2><p class="c1"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">In this section of the book, we focus on learning tools that will you a more effective programmer. In order to follow along with some of the examples in this section of the book, you need access to a computer running the operating system Ubuntu. If you are already using Ubuntu, you are all set. If not, you are going to have to install a virtual machine if you want to follow along with all of the examples (which you should do). Virtual machines are used all the time in software development, so it&rsquo;s a good idea to learn how to set one up regardless of what operating system you are using. The reason we are using Ubuntu is because almost every server in the world uses Linux, and learning to use it will make you a better programmer. &nbsp; </span></p><p class="c0"><span class="c3">A virtual machine is software that emulates hardware. For example, you can use a virtual machine &nbsp;to run another operating system on the operating system you already are using. That means you can say, run Linux on a Windows machine. </span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;We are going to use a free program called VirtualBox to set up a new virtual machine running Ubuntu. Because the instructions for setting up a virtual machine on VirtualBox is liable to change at any time, rather than include them in the book, I put the instructions on GitHub so I can easily make changes when needed. The instructions are available at: </span><span class="c17 c3"><a class="c18" href="https://www.google.com/url?q=https://github.com/calthoff/tstp/blob/master/virtual_machine_setup/setup.txt&amp;sa=D&amp;ust=1467337426779000&amp;usg=AFQjCNGOuviYahiFnhCFaqChpqjiNyCdpg">https://github.com/calthoff/tstp/blob/master/virtual_machine_setup/setup.txt</a></span></p><p class="c1"><span class="c3">Once you follow the instructions and set up a Virtual Machine, you will be all set to follow all of the examples in this section. </span></p><p class="c1 c9"><span></span></p><h2 class="c1 c13" id="h.6t6s5xazb26u"><span>The Command Line</span></h2><p class="c1"><span class="c3">UNIX was not designed to stop its users from doing stupid things, as that would also stop them from doing clever things.</span></p><p class="c1"><span class="c3">&mdash; Doug Gwyn</span></p><p class="c1 c9"><span class="c3 c5"></span></p><p class="c1"><span class="c3">The command line is the interface you use to interact with your operating system. Anything you can do with your operating system&rsquo;s GUI (graphical user interface), you can do with the command line, plus a whole lot more. </span></p><p class="c0"><span class="c3">Using the command line is an important part of programming. You can use the &nbsp;command line to interact with servers, schedule processes, edit files &nbsp;and much more. In this chapter we will get familiar with the command line by going over go over some of the commands you will use the most.</span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.60xxv3thb2e"><span>Getting Setup</span></h3><p class="c1"><span class="c3">To follow along this chapter, you should be using either a OS X (Mac) or Ubuntu. If you do not have a computer running either of these, you should use Ubuntu running on VirtualBox. &nbsp;</span></p><p class="c1"><span class="c3">You can find the command line by searching for &ldquo;terminal&rdquo; from spotlight search on a Mac and choosing the first result. Do the same on Ubuntu, from the icon &ldquo;Search your computer and online resources&rdquo;. &nbsp;We also are going to need a file to play with. Use your favorite text editor to create a file called self_taugt.txt and add the text &ldquo;Hello World&rdquo; to it. </span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.h19tr3dl2wcm"><span>e</span><span>cho</span></h3><p class="c1"><span class="c3">The command line has it&rsquo;s own language we are going to learn to use in this chapter. We use this language to enter commands, and the command line executes them, just like the Python Shell. For example, the command echo prints to the terminal, similar to the </span><span class="c4">print()</span><span class="c3">&nbsp;function in Python:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c0"><span class="c4">&nbsp;$ &nbsp;echo Hello World </span></p><p class="c0"><span class="c4">&nbsp;&gt;&gt; Hello World</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">Unix-like operating systems have their own programming language called Bash. </span><span class="c4">echo Hello World</span><span class="c3">&nbsp;is an example of programming in Bash. &nbsp;</span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.gvjmjpn547m"><span>Navigating</span></h3><p class="c0"><span class="c3">When you open up the command line, it will start in a specific directory on your computer, usually your home directory. A directory is a folder on your computer. &nbsp;Use the command </span><span class="c4">pwd</span><span class="c3">&nbsp;to print the directory you are currently in:</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c4">$ pwd</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; /Users/coryalthoff/stp/my_project</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c4">&nbsp;pwd </span><span class="c3">stands for print working directory, and will print something like &ldquo;/Users/coryalthoff&rdquo;, depending on the folder you are in. &nbsp;We can switch directories with the command </span><span class="c4">cd</span><span class="c3">, which stands for change directory:</span></p><p class="c0 c9"><span class="c3"></span></p><p class="c1 c10"><span class="c4">$ cd Document &nbsp;</span></p><p class="c1 c10"><span class="c4">$ pwd</span></p><p class="c1 c10"><span class="c4">&gt;&gt; /Users/coryalthoff/document</span><span class="c3">s</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">As you can see, you are now in the Documents directory. &nbsp;</span><span class="c4">ls</span><span class="c3">&nbsp;prints the contents of the directory you are in:</span></p><p class="c0"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p><p class="c0"><span class="c4">$ ls</span></p><p class="c0"><span class="c4">&gt;&gt; my_document1.txt&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp;my_document2.txt</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">It will print any files, or folders that exist in the current directory.</span></p><p class="c1"><span class="c3">&nbsp;</span></p><p class="c1"><span class="c3">The command </span><span class="c4">cd ..</span><span class="c3">&nbsp;takes you back one directory:</span></p><p class="c0 c9"><span class="c3"></span></p><p class="c0"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c4">$ cd ..</span></p><p class="c0"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$ pwd</span></p><p class="c0"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; /Users/coryalthoff</span></p><p class="c0 c9"><span class="c3"></span></p><p class="c1"><span class="c3">The command </span><span class="c4">mkdir</span><span class="c3">&nbsp;creates a new directory:</span></p><p class="c0 c9"><span class="c3"></span></p><p class="c0"><span class="c4">$ mkdir tstp</span></p><p class="c0"><span class="c4">&gt;&gt; </span></p><p class="c0 c9"><span class="c3"></span></p><p class="c1"><span class="c3">Enter the new directory with:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c0"><span class="c4">$ cd tstp</span></p><p class="c0"><span class="c4">&gt;&gt;</span></p><p class="c0 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;If you enter </span><span class="c4">pwd</span><span class="c3">&nbsp;you will see we are in the new directory </span><span class="c4">tstp</span><span class="c3">, and &nbsp;</span><span class="c4">ls</span><span class="c3">&nbsp;will not print anything because the new directory is empty:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c4">$ pwd</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$ ls</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; /Users/coryalthoff/tstp</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; </span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.p68p7r198w0o"><span>history</span></h3><p class="c1"><span class="c3 c5">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">You can scroll through your recent commands by pressing the up and down arrows at the command line. To see even more of your recent commands, use the command </span><span class="c4">history</span><span class="c3">&nbsp;. </span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c4">$ history</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; 1. echo hello world</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; 2. pwd</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; 3. ls</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&hellip;</span></p><p class="c1 c9"><span class="c4"></span></p><h3 class="c1 c13" id="h.1p2wmwhuj4ht"><span>Relative vs Absolute</span></h3><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;In addition to using </span><span class="c4">cd [directory] </span><span class="c3">to navigate to a new directory, you can also use </span><span class="c4">cd [directory_path]</span><span class="c3">&nbsp;to navigate to a specific location on your computer. You can do this by using the </span><span class="c4">cd</span><span class="c3">&nbsp;command followed by the path you want to go to. There are two ways of expressing the location you want to go to. The first way is called an absolute path. An absolute path always starts with </span><span class="c4">/</span><span class="c3">. On </span><span class="c3">Unix-like</span><sup><a href="#cmnt29" id="cmnt_ref29">[ac]</a></sup><span class="c3">&nbsp;operating systems, </span><span class="c4">/ </span><span class="c3">represents the root directory of the operating system: the top level directory.The folders in your operating system are a tree. A tree is an important concept in Computer Science called a data structure (covered in Part IV). In a tree, there is a </span><span class="c15 c3">root</span><span class="c3">&nbsp;at the top. The </span><span class="c15 c3">root</span><span class="c3">&nbsp;can have branches, and each one of the branches can have more branches, and those branches can have branches. This goes on indefinitely. </span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">[add illustration of a tree data structure]</span><sup><a href="#cmnt30" id="cmnt_ref30">[ad]</a></sup></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">Navigate to the root directory by typing </span><span class="c4">cd /</span><span class="c3">&nbsp;into the terminal:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c4">$ cd /</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;You are now in your root directory. If you enter the command </span><span class="c4">ls</span><span class="c3">, you will see the folders your operating system uses to run:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c4">$ ls</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; Applications &nbsp;Volumes &nbsp;home &nbsp; etc&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bin&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">Folders like </span><span class="c4">etc</span><span class="c3">, </span><span class="c4">bin</span><span class="c3">&nbsp;and </span><span class="c4">var</span><span class="c3">&nbsp;are created by the operating system &nbsp;on Unix-like machines. If you try going back one directory, you won&rsquo;t be able to:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c4">$ cd ..</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$ ls</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; /</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">You are still in the root directory because the root directory is the top level directory. It is the root of the tree. There is no going backwards from here. When you use an absolute path, you are giving the path starting from this location. </span><span class="c4">/</span><span class="c3">&nbsp;is an example of an absolute path. Another example of an absolute path is </span><span class="c4">/var</span><span class="c3">. Navigate there with:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c0"><span class="c4">$ cd /var</span></p><p class="c0"><span class="c4">$ pwd</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; /var</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">The absolute path </span><span class="c4">/var</span><span class="c3">&nbsp;starts refers to a directory called </span><span class="c4">var</span><span class="c3">&nbsp;inside the </span><span class="c4">root</span><span class="c3">&nbsp;directory. The path </span><span class="c4">/var/Document</span><span class="c3">s would look for a directory called </span><span class="c4">Documents</span><span class="c3">&nbsp;inside the directory </span><span class="c4">var</span><span class="c3">&nbsp;inside the </span><span class="c4">root</span><span class="c3">&nbsp;directory. </span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;The other way of specifying a location on your computer is called using a relative path. You can navigate to a relative path with </span><span class="c4">cd [relative path]</span><span class="c3">. Instead of starting at the </span><span class="c15 c3">root</span><span class="c3">, the path is relative to your current location (the directory you are in). Unlike an absolute path, a relative path does not start from the root directory, it starts from the directory you are currently in. So if you are currently in the directory </span><span class="c4">root</span><span class="c3">, and want to navigate to </span><span class="c4">var</span><span class="c3">, you would use the path </span><span class="c4">var</span><span class="c3">. Absolute paths do not start from the root directory, so you do not start them with </span><span class="c4">/</span><span class="c3">. They start from the the directory you are currently in, so you start with the next directory you want to go to. So for example if you wanted to go from the root directory to var/tmp you would use cd var/tmp whereas the absolute path would be /var/tmp. </span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.q2j31jn2qebw"><span>View A File With less</span></h3><p class="c0"><span class="c3">The </span><span class="c4">less</span><span class="c3">&nbsp;command lets you view files from the command line. Pick a file on your computer and use the command </span><span class="c4">less [filename] </span><span class="c3">to view the file from the command line. Press </span><span class="c4">q</span><span class="c3">&nbsp;to exit. </span></p><p class="c0 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.hlxe8ozewnge"><span>Users</span><sup><a href="#cmnt31" id="cmnt_ref31">[ae]</a></sup></h3><p class="c1"><span class="c3 c5">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">Operating systems can have different users. Users can have different levels of permissions, and might see different directories and files when they log in. You can create a new user from the command line with the syntax </span><span class="c4">adduser [username]</span><span class="c3">:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c4">$ adduser tstp</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt;</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">You can change users with the command </span><span class="c4">su [username]</span><span class="c3">: </span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$ su tstp</span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.jg9947bx97nz"><span>sudo</span><sup><a href="#cmnt32" id="cmnt_ref32">[af]</a></sup></h3><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;The &nbsp;root user is the highest level user. The root user has permission to do an anything, for example creating or deleting users. Every system has a root user. For security reasons, you normally do not log in as the root user. Instead you use a command called </span><span class="c4">sudo</span><span class="c3">&nbsp;to temporarily use the power of the root user to issue a command. </span></p><p class="c0"><span class="c4">sudo</span><span class="c3">&nbsp;allows you to do most things as the root user, but not everything. For example, there are certain files you cannot delete even if you use </span><span class="c4">sudo</span><span class="c3">. &nbsp;</span><span class="c4">sudo</span><span class="c3">&nbsp;is configured in a file located at &nbsp;</span><span class="c4">/etc/sudoers</span><span class="c3">. Interestingly, you cannot view the file without using </span><span class="c4">sudo</span><span class="c3">. </span><span class="c4">cd</span><span class="c3">&nbsp;to </span><span class="c4">/etc/sudoers</span><span class="c3">&nbsp;and checkout the sudoers file by using sudo with the less command:</span></p><p class="c0"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p><p class="c0"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c4">$ cd /etc/sudoers</span></p><p class="c0 c10"><span class="c4">$ sudo less sudoers </span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">Make sure to be careful using </span><span class="c4">sudo</span><span class="c3">. Generally, you should never run a program using </span><span class="c4">sudo</span><span class="c3">, because if the program is compromised, the attackers will have root control.</span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.66p418cud0eq"><span>Permissions</span><sup><a href="#cmnt33" id="cmnt_ref33">[ag]</a></sup></h3><p class="c1"><span class="c3">Every directory and file on your computer has a set of permissions. These permissions determine which users are allowed to do what to the file. There are three types of permissions: &ldquo;r&rdquo;, &ldquo;rw&rdquo; and &ldquo;x&rdquo;, which stand for read, write and executable.</span></p><p class="c0"><span class="c3">You can view a file or folder&rsquo;s permissions with the command </span><span class="c4">ls -lah [name_of_file]</span><span class="c3">. The result looks something like this:</span></p><p class="c0 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c4">-rw-r--r-- &nbsp;1 coryalthoff &nbsp;staff &nbsp; &nbsp; 5B Feb 21 11:55 test.py</span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.7emxiu9mqc2k"><span>Change Permissions</span><sup><a href="#cmnt34" id="cmnt_ref34">[ah]</a></sup></h3><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;You can add and subtract permissions with the command </span><span class="c4">chmod</span><span class="c3">. You can add permission to read with the command</span><span class="c3 c5">&nbsp;</span><span class="c4">chmod +r</span><span class="c3">, permission to read and write with the command </span><span class="c4">chmod +rw</span><span class="c3">, and permission to execute with </span><span class="c4">chmod +x</span><span class="c3">. Similarly, you can subtract the same permissions with </span><span class="c4">chmod -r</span><span class="c3">, </span><span class="c4">chmod -w</span><span class="c3">&nbsp;and </span><span class="c4">chmod -x</span><span class="c3">, respectively. </span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.hwmgff64angj"><span>Executables</span></h3><p class="c1"><span class="c3">You can easily create and run a bash script from the command line. Create a new file called hello_world.sh. In the new file type </span><span class="c4">echo Hello World</span><span class="c3">. Try the command </span><span class="c4">./hello_world.sh</span><span class="c3">. </span></p><p class="c1"><span class="c4">/</span><span class="c3">&nbsp;is used to run a script and </span><span class="c4">. </span><span class="c3">means run the script from the current directory. You could also replace the period with the path to the file. When you do this, your console should print &ldquo;Permission denied&rdquo;. </span></p><p class="c1"><span class="c3">When we create a script, we need to change the permissions on it so we can run it. Change the permission of the script with the command </span><span class="c4">chmod +x hello_world.sh</span><span class="c3">. Run it again with </span><span class="c4">./hello_world.sh</span><span class="c3">. Your terminal should print &ldquo;Hello World&rdquo;.</span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.cui36gbc9lsu"><span>pipes </span></h3><p class="c1"><span class="c3">In Unix-like systems, the character </span><span class="c4">| </span><span class="c3">is known as a pipe. You can use a pipe to take the output of one command and input it into another command. You are not limited to two commands. You can chain as many commands together with pipe symbols as you want. &nbsp;</span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.zi5dmafyk54k"><span>Jump Around&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></h3><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;There inevitably will be times when you want to edit a command already typed out on the command line. Your first instinct will be to use the arrow right or left key to get to the place you want to change. The problem is this is slow. Instead, there are a few shortcuts that will get you there faster. Use </span><span class="c4">esc b</span><span class="c3">&nbsp;to jump back a word and </span><span class="c4">esc f </span><span class="c3">to jump forward a word. You can also jump to the beginning of the line with </span><span class="c4">control a</span><span class="c3">&nbsp;or the end with </span><span class="c4">control e</span><span class="c3">. </span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.ze01lavdqpac"><span>Tab Complete</span></h3><p class="c1"><span class="c3 c5">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">Tab complete is a feature on most operating systems that will help improve the speed you get things done from the command line. If you are in the middle of typing in the command line and press tab, the command line will try to autocomplete for you. Try it for yourself by typing </span><span class="c4">gre</span><span class="c3">&nbsp;in the command line followed by </span><span class="c4">tab</span><span class="c3">. The command line turns </span><span class="c4">gre</span><span class="c3">&nbsp;into </span><span class="c4">grep</span><span class="c3">. You can also tab complete paths. Enter </span><span class="c4">pwd</span><span class="c3">&nbsp;and then start typing the path of the directory you are in and finish it with tab complete.</span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.sz1zima4qa4z"><span>c</span><span>at</span></h3><p class="c1"><span class="c3 c5">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c4 c5">c</span><span class="c4">at</span><span class="c3">&nbsp;is a commonly used, versatile command. You can use </span><span class="c4">cat</span><span class="c3">&nbsp;to display the contents of a file from the command line. If you use cat on the text file we created at the beginning of the chapter with </span><span class="c4">cat self_taught.txt</span><span class="c3">, &ldquo;Hello World&rdquo; will print in the terminal. </span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Cat stands for catenate which means &ldquo;to connect in a series&rdquo; 11. We will use the cat command to demonstrate catenating two files. First, create a file called c1.txt and add the first line &ldquo;file 1 contents&rdquo;. Next, create a file called c2.txt with the first line as &nbsp;&ldquo;file 2 contents.&rdquo;. Use the command &ldquo;cat c1.txt c2.txt &gt; combined.txt&rdquo; to concatenate the two files. Open up combined.txt to see the results. </span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.hl56hkbp64it"><span>Wildcard</span></h3><p class="c1"><span class="c3">A wildcard is a character used to match certain numbers of other characters. Two examples of wildcards are an asterisk and a question mark. The asterisk wildcard matches one or more other characters. &nbsp;A common example of using the asterisk wildcard is with the command ls. For example, the command &ldquo;ls *.txt&rdquo; will match any files with &ldquo;.txt&rdquo; in them. </span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.kq114m6usgxn"><span>Hidden Files</span></h3><p class="c1"><span class="c3 c5">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">Hidden files in unix-like systems start with a period, for example &ldquo;.hidden&rdquo; You will not see hidden files using the command &ldquo;ls&rdquo; unless you add the flag &ldquo;-a&rdquo; ie. &ldquo;ls -a&rdquo;. For practice, try creating a hidden file named &ldquo;.self_taught&rdquo; with a text editor and testing if you can see it with &ldquo;ls&rdquo; and &ldquo;ls -a&rdquo;.</span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.2wnhuze4oy72"><span>Environmental Variables</span></h3><p class="c0"><span class="c3">Your operating system can store and retrieve variables. These are called environmental variables. When you use the Python web framework Django, you set an environmental variable to let Django know where your Django settings file is located. </span></p><p class="c0"><span class="c3">You can create a new environmental variable from the command line with the syntax&ldquo;export [define variable]&rdquo;. For example:</span></p><p class="c0 c9"><span class="c3"></span></p><p class="c0"><span class="c3">$ export x=100</span></p><p class="c0"><span class="c3">$ echo $x</span></p><p class="c0"><span class="c3">&gt;&gt; 100</span></p><p class="c0 c9"><span class="c3"></span></p><p class="c1"><span class="c3">When you reference environmental variables, you put a dollar sign &ldquo;$&rdquo; in front of the variable name.</span></p><p class="c0"><span class="c3">Creating an environmental variable from the command line is not permanent. If you quit the command line, open it again and type &ldquo;echo $x&rdquo;, it will no longer print &ldquo;100&rdquo;. </span></p><p class="c1"><span class="c3">We can make x persistent by adding it to our &ldquo;.profile&rdquo; file. Cd to your home directory with &ldquo;cd ~&rdquo; and open the file &ldquo;.profile&rdquo; with vim ie. &ldquo;vim .profile&rdquo;. Type &ldquo;export =100&rdquo; into the file and exit with &ldquo;:x&rdquo;. (don&rsquo;t forget to press i or insert to be able to type). Close and reopen your command line, and try:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$ echo $x</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; 100</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;It should now print out &ldquo;100&rdquo;.</span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.h6lfnmhvatol"><span>$PATH</span></h3><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;When I used to install programs, I would occasionally try to run them from the command line only to be greeted with a message saying the program does not exist. The key to fixing this is to understand what happens when you run a command that is not built into your operating system. </span></p><p class="c0"><span class="c3">If you enter the command &ldquo;self&rdquo; into the terminal, you will get an error saying the command self was not found. When you type a non built in command into the terminal, it checks to see if it is in any of the locations stored an environmental variable called $PATH. For example, built in commands are usually found in the folder &ldquo;/usr/bin&rdquo;. Your path may look something like this &ldquo;/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin&rdquo;. Folders are separated by a &ldquo;:&rdquo;. In this example, &ldquo;/usr/bin&rdquo; is in our path, in between &ldquo;/usr/local/bin&rdquo; and &ldquo;/bin&rdquo;. The path does not have to be a folder. It can also be a direct path to the program like &ldquo;home/my_command&rdquo;.</span></p><p class="c0"><span class="c3">If the command is found in &nbsp;$PATH, the terminal will run the program linked to the variable stored in $PATH for the command you typed. Use echo to print your $PATH:</span></p><p class="c0"><span class="c3">&nbsp;</span></p><p class="c0"><span class="c3">$ echo $PATH</span></p><p class="c1 c10"><span class="c3">&gt;&gt;/Library/Frameworks/Python.framework/Versions/3.5/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin :/sbin</span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.r2psbwpiyc4s"><span>w</span><span>hich</span></h3><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;You can check if a program is there with the </span><span class="c4">which</span><span class="c3">&nbsp;command. For example, if you type </span><span class="c4">which grep</span><span class="c3">&nbsp;in the terminal, you will see a path to the built in program </span><span class="c4">grep</span><span class="c3">:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$ which grep</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; /usr/bin/grep</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">If you use the </span><span class="c4">which</span><span class="c3">&nbsp;command on something made up, like </span><span class="c4">which bla123</span><span class="c3">, nothing will print because there is no path for that command. </span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.qlfxdseo880l"><span>SSH</span></h3><p class="c1"><span class="c3 c5">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">SSH stands for secure shell. SSH provides a secure way for a client to connect to a server. For example, you can use the SSH command from your terminal (the client) to connect to a server. If you want to connect to a server at the ip address 192.0.2.1, you can do so using &ldquo;ssh root@192.0.2.1&rdquo;. </span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.xogdw2ont4q7"><span>cron</span></h3><p class="c1"><span class="c4">c</span><span class="c4">ron</span><span class="c3">&nbsp;is a program on Unix-like operating systems used to schedule programs to run at specific dates and times. </span><span class="c4">cron</span><span class="c3">&nbsp;is named after the Greek word for time, &ldquo;chronos&rdquo;.</span></p><p class="c1"><span class="c3">cron is a daemon. A daemon is a process running in the background of your operating system. A web server is another example of a daemon. It&rsquo;s a process running in the background that doesn&rsquo;t do anything until an http request comes in from a client. Likewise, Cron is a daemon that sits around until the time and date matches the time and date of one of the programs it&rsquo;s scheduled to run. </span></p><p class="c1"><span class="c3 c5">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">In this section, we will schedule a script to run every minute of every day using cron. </span><sup><a href="#cmnt35" id="cmnt_ref35">[ai]</a></sup></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.6nssgqwwgess"><span>Build a Command Line Program</span></h3><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;In this section we will be opening up the black box of creating commands. First, open up your Ubuntu virtual machine on VirtualBox. Use apt-get to install Vim with &ldquo;sudo apt-get install vim&rdquo;. Create a new file called hello_world with &ldquo;vim hello_world&rdquo;. The first thing we need to do inside the file is add a shebang. Add the shebang &ldquo;#!/bin/sh&rdquo; as the first line of our new file. On the second line of the file, add &ldquo;echo Hello World&rdquo;. Go ahead and save your changes and exit. Make the script executable by adding an executable permission with &ldquo;chmod +x hello&rdquo;. The last thing we need to do is move the script somewhere on our path. Remember, you can view your path with echo $PATH. Move our script to /usr/bin with &ldquo;sudo cp hello /usr/bin&rdquo;. That is all there is to it. We just created a new command. Run your new command from anywhere in the terminal with &ldquo;hello_world&rdquo;. </span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.mgistxt5xhod"><span>Other Tools</span></h3><p class="c1"><span class="c3 c5">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">If your terminal gets cluttered, you can clear it with the command &ldquo;clear&rdquo;. If a process is taking too long in the command line, you can kill it with control+c. </span></p><p class="c1"><span class="c3">Another powerful command is &ldquo;grep&rdquo; which is used to search files. For example &ldquo;grep hello my_file.txt&rdquo; will search the file my_file.txt for anything matching &ldquo;hello&rdquo;. Grep uses regular expressions, which are covered later in the chapter Regular Expressions and allow you to do more powerful matching. &nbsp;</span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.ce6yh38q0rp"><span>Wrapping Up</span></h3><p class="c1"><span class="c3">This chapter only scratched the surface of the power of the command line. The command line is filled with powerful tools and too many commands to fit into this book. In this chapter I tried to give you the 20 percent of commands you will use 80 percent of the time. A lot of programming is looking things up. I&rsquo;ve included further resources on the command line at the end of the book. Hopefully you learned enough to start feeling comfortable with the command line, and to continue learning more about it. Becoming a command line ninja will make you a much more effective programmer.</span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.g1zy03v5xan"><span>The One Week Challenge</span></h3><p class="c1"><span class="c3">To get better at using the command line, I challenge you to use nothing but the command line for one week. That means no GUI for anything other than using the internet! You should complete this challenge during a week you are actively working on a programming project. </span></p><p class="c1 c9"><span class="c3"></span></p><h2 class="c1 c13" id="h.qdmtlgv3pxw"><span>vim</span></h2><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Vim is a command line text editor. In other words, it&#39;s like Microsoft Word, except you use it from the command line. Vim is one of many command line text editors like Emacs. Vim &nbsp;is useful in several situations. Servers don&rsquo;t have GUIs (graphical user interface&rsquo;s), so if you want to make a change to a file on a server, you will need to use a command line text editor like Vim. It is also often faster to make changes to a file on your local machine with Vim than trying to open up an editor with a GUI like Microsoft Word. </span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.38wjjbt1qqhm"><span>Setup</span></h3><p class="c1"><span class="c3">If you are using Ubuntu, first install vim with the command &nbsp;</span><span class="c4">apt-get install vim</span><span class="c3">. Make sure to enter </span><span class="c4">Y</span><span class="c3">&nbsp;when prompted. </span></p><p class="c1 c9"><span></span></p><h3 class="c1 c13" id="h.5cfee6wegot"><span>Create a File</span></h3><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;You can create a new file with vim by going to the command line and using the command </span><span class="c4">vim [name of the file to create]</span><span class="c3">. Use the command </span><span class="c4">vim self_taught.txt</span><span class="c3">&nbsp;to create a new text file called </span><span class="c4">self_taught.txt</span><span class="c3">. Copy and paste the following post by Cranley on StackExchange answering the question &ldquo;</span><span class="c17 c3"><a class="c18" href="https://www.google.com/url?q=http://programmers.stackexchange.com/questions/44177/what-is-the-single-most-effective-thing-you-did-to-improve-your-programming-skil&amp;sa=D&amp;ust=1467337426868000&amp;usg=AFQjCNGj9bwpYP_x3eKHyeSMu0gND2Re6A">What is the single most effective thing you did to improve your programming skills?&rdquo;</a></span></p><p class="c1"><span class="c3">&nbsp;to your file:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c10"><span class="c3">in no specific order&hellip;</span></p><p class="c1 c10"><span class="c3">Working with people far smarter than myself</span></p><p class="c1 c10"><span class="c3">Always listening to what others have to say, regardless if they&#39;re junior, intermediate, senior or guru. job title doesn&#39;t mean anything.</span></p><p class="c1 c10"><span class="c3">Learning other frameworks/languages, and seeing how they do things, and compare that to stuff that I already know</span></p><p class="c1 c10"><span class="c3">Reading about patterns, best practices, and then examining my old stuff and applying those patterns where necessary</span></p><p class="c1 c10"><span class="c3">Pair programming</span></p><p class="c1 c10"><span class="c3">Disagreeing with everything Joel says. ;)</span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.86a2yktzg4mf"><span>Modes</span></h3><p class="c1"><span class="c3 c5">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">Inside your new file, if you hit a key like </span><span class="c4">b</span><span class="c3">, nothing will happen. This is because vim has different modes and the mode vim starts in, Normal Mode, is not meant for adding text to the file&mdash;you can delete text in normal mode, but you cannot insert new text.</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;To add new text, Inside Vim, enter Insert Mode by pressing </span><span class="c4">insert</span><span class="c3">. Now you can add text to the file like a normal word processor. Press control-c to return to Normal Mode. </span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.e7zi9dalv15c"><span>Jump Around</span></h3><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Since you can&rsquo;t use your mouse to move the cursor around with vim; it&rsquo;s important to learn a few shortcuts to jump to different locations in your document, so that you don&rsquo;t end up using the arrow keys on your keyboard (because it&rsquo;s slow). To practice moving around, first make sure you are in Normal Mode. Then open up the file we created </span><span class="c4">self_taught.txt</span><span class="c3">. You can move to the beginning of a word by pressing </span><span class="c4">b</span><span class="c3">&nbsp;and the end of a word with </span><span class="c4">a</span><span class="c3">. </span><span class="c4">0 </span><span class="c3">will move you to the beginning of the line you are on, while the dollar sign </span><span class="c4">$</span><span class="c3">&nbsp;will move you to the end of the line. </span><span class="c4">H</span><span class="c3">&nbsp;will move you to the first line of the page and </span><span class="c4">L</span><span class="c3">&nbsp;will move you to the last. Spend some time using these keys to get familiar with with navigating through a file using vim. </span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.d76n05v2mdc1"><span>Deleting</span><span>&nbsp;</span></h3><p class="c1"><span class="c3">You can delete entire lines of text in normal mode by pressing the </span><span class="c4">d</span><span class="c3">&nbsp;key twice. </span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.llieczr4ay8s"><span>Exiting Vim</span></h3><p class="c1"><span class="c3">To exit vim you need to first switch to Normal Mode by pressing </span><span class="c4">control-c</span><span class="c3">. Next press the </span><span class="c4">shift</span><span class="c3">&nbsp;key and then hit the </span><span class="c4">colon</span><span class="c3">&nbsp;key (while still holding the shift key). From here you can either enter </span><span class="c4">q</span><span class="c3">&nbsp;to quit and save your changes, or </span><span class="c4">q!</span><span class="c3">&nbsp;to quit without saving them. Once you&rsquo;ve entered either </span><span class="c4">q</span><span class="c3">&nbsp;or </span><span class="c4">q!</span><span class="c3">, press the enter key to exit. Here are more </span><span class="c3">ways</span><span class="c3">&nbsp;to exit:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c0"><span class="c3">:q to quit (short for :quit)</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:q! to quit without saving (short for :quit!)</span></p><p class="c0"><span class="c3">:wq to write and quit (think write and quit)</span></p><p class="c1 c10"><span class="c3">:wq! to write and quit even if file has only read permission (if file does not have write permission: force write)</span></p><p class="c0"><span class="c3">:x to write and quit (shorter than :wq)</span></p><p class="c0"><span class="c3">:qa to quit all (short for :quitall)</span></p><p class="c1"><span class="c41 c3">&nbsp; 5</span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.26aksbb9uush"><span>Wrapping Up</span></h3><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;In this chapter we went over the basics of using the command line text editor vim. We went over some of the basic commands you will use on a daily basis. There is a whole lot more to vim, and I encourage you to continue learning to use either vim or another command line text editor, as it will make you a more efficient programmer. </span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.ur8l56w7mgle"><span>VimTutor Challenge</span></h3><p class="c1"><span class="c3">I challenge you to go to your command line, enter </span><span class="c4">vimtutor</span><span class="c3">&nbsp;and see what happens. &nbsp;</span></p><p class="c1 c9"><span class="c3"></span></p><h2 class="c1 c13" id="h.i7w0bdaedvos"><span>Regular Expressions</span></h2><p class="c1"><span class="c3">&ldquo;Some people, when confronted with a problem, think &quot;I know, I&#39;ll use regular expressions.&quot; Now they have two problems.&rdquo;</span></p><p class="c1"><span class="c3">- Jamie Zawinski</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;You may have noticed this chapter titled Regular Expressions begins with a quote seemingly disparaging regular expressions. You might be thinking, &ldquo;If using regular expressions causes additional problems, maybe I shouldn&rsquo;t use them at all&rdquo;. First let&#39;s get into what regular expressions are, then we will dive into the meaning of this popular quote. Regular expressions are used to define and search for patterns in strings. As for the quote, the blog Coding Horror has an excellent post I recommend you read, found at </span><span class="c17 c3"><a class="c18" href="https://www.google.com/url?q=http://blog.codinghorror.com/regular-expressions-now-you-have-two-problems/&amp;sa=D&amp;ust=1467337426881000&amp;usg=AFQjCNFwyC21hisx6APFn5zYLnl790lRsg">http://blog.codinghorror.com/regular-expressions-now-you-have-two-problems/</a></span><span class="c3">, explaining the meaning of the quote.</span><span class="c3">&nbsp;The post compares regular expressions to hot sauce, explaining while regular expressions are great occasionally, using them too much is problematic because they are difficult to read, and if your code is filled with them, it will quickly become unreadable. Nonetheless, regular expressions can be incredibly useful. In this chapter we are going to practice using regular expressions from the command line using grep, a command for using regular expressions to search files. We will also use regular expressions in Python. </span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.vvpk46adrjfr"><span>Setup</span></h3><p class="c1"><span class="c3 c5">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">In this chapter we are going to practice using regular expressions from the command line using grep. In order to do this, create a file called </span><span class="c4">zen.txt</span><span class="c3">. Copy and paste the following text, known as the Zen of Python into the file. I also recommend reading it as it contains some great wisdom:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c10"><span class="c3">The Zen of Python</span></p><p class="c1 c10"><span class="c3">Beautiful is better than ugly.</span></p><p class="c1 c10"><span class="c3">Explicit is better than implicit.</span></p><p class="c1 c10"><span class="c3">Simple is better than complex.</span></p><p class="c1 c10"><span class="c3">Complex is better than complicated.</span></p><p class="c1 c10"><span class="c3">Flat is better than nested.</span></p><p class="c1 c10"><span class="c3">Sparse is better than dense.</span></p><p class="c1 c10"><span class="c3">Readability counts.</span></p><p class="c1 c10"><span class="c3">Special cases aren&#39;t special enough to break the rules.</span></p><p class="c1 c10"><span class="c3">Although practicality beats purity.</span></p><p class="c1 c10"><span class="c3">Errors should never pass silently.</span></p><p class="c1 c10"><span class="c3">Unless explicitly silenced.</span></p><p class="c1 c10"><span class="c3">In the face of ambiguity, refuse the temptation to guess.</span></p><p class="c1 c10"><span class="c3">There should be one-- and preferably only one --obvious way to do it.</span></p><p class="c1 c10"><span class="c3">Although that way may not be obvious at first unless you&#39;re Dutch.</span></p><p class="c1 c10"><span class="c3">Now is better than never.</span></p><p class="c1 c10"><span class="c3">Although never is often better than *right* now.</span></p><p class="c1 c10"><span class="c3">If the implementation is hard to explain, it&#39;s a bad idea.</span></p><p class="c1 c10"><span class="c3">If the implementation is easy to explain, it may be a good idea.</span></p><p class="c1 c10"><span class="c3">Namespaces are one honking great idea -- let&#39;s do more of those!</span></p><p class="c1 c9 c10"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;On Ubuntu, when you use grep, it highlights the word matched. On OSX, this behavior does not happen by default. If you are using a Mac, we can set some environmental variables to fix this. Set the following environmental variables in the terminal:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c10"><span class="c4">$ export GREP_OPTIONS=&#39;--color=always&#39;</span></p><p class="c1 c10"><span class="c4">$ export GREP_COLOR=&#39;1;35;40&#39;</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">Remember, setting an environmental variable from the terminal is not permanent, so if you exit the terminal and come back you will have to set the environmental variables again. Add the environmental variables to your </span><span class="c4">.profile </span><span class="c3">file if you want to make the change permanent. </span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.bt2t65vzi03f"><span>Simple Match</span></h3><p class="c1"><span class="c3">The simplest pattern you can match with a regular expression is a direct match of a string ie. the regular expression </span><span class="c4">Beautiful</span><span class="c3">&nbsp;will find a match in the string </span><span class="c4">&ldquo;Beautiful&rdquo;</span><span class="c3">. From the command line, in the directory where you created the file </span><span class="c4">zen.txt</span><span class="c3">, enter the command:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c4">$ grep Beautiful zen.txt</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; </span><span class="c4 c73">Beautiful</span><span class="c4">&nbsp;is better than ugly.</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;Your console should print out the line </span><span class="c4">Beautiful is better than ugly</span><span class="c3">. with </span><span class="c4">Beautiful</span><span class="c3">&nbsp;highlighted because it is the word the regular expression matched.</span></p><h3 class="c1 c13" id="h.sdw8swac5nvj"><span>Ignore Case</span></h3><p class="c1"><span class="c3">If we change </span><span class="c4">Beautiful</span><span class="c3">&nbsp;to </span><span class="c4">beautiful</span><span class="c3">, we no longer will get a match. Try </span><span class="c4">grep beautiful zen.py</span><span class="c3">&nbsp;to see for yourself. If we want to match the word </span><span class="c4">beautiful</span><span class="c3">&nbsp;regardless of case, we can use the flag </span><span class="c4">-i</span><span class="c3">:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$ grep -i beautiful zen.txt</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; </span><span class="c4 c73">Beautiful</span><span class="c4">&nbsp;is better than ugly.&rdquo;</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">Because we added the flag </span><span class="c4">-i </span><span class="c3">to our command, </span><span class="c4">grep -i beautiful zen.txt</span><span class="c3">&nbsp;will ignore case and match again. </span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.p1up3kjutvp6"><span>Only Return Matched</span></h3><p class="c1"><span class="c3 c5">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">As you can see, grep is returning the entire line of the file the match was found on. </span><span class="c3">We can return the exact word matched by using the flag </span><span class="c4">-o</span><span class="c3">:</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c4">$ grep -o Beautiful zen.txt</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; </span><span class="c4 c73">Beautiful</span></p><p class="c1"><span class="c3">&nbsp; </span></p><h3 class="c1 c13" id="h.t64otbmjyh2l"><span>Match Beginning and End</span></h3><p class="c1"><span class="c3 c5">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">Regular expressions have reserved special characters that don&rsquo;t match themselves, but instead do something special. They are called anchor matches and they specify where a match needs to take place in the text in order for it to be valid. For example, the </span><span class="c4">^</span><span class="c3">&nbsp;character is used to look for matches only if they occur at the beginning of a line:</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p><p class="c0"><span class="c4">$ grep ^If zen.txt</span></p><p class="c0"><span class="c4">&gt;&gt; </span><span class="c4 c73">If</span><span class="c4">&nbsp;the implementation is hard to explain, it&#39;s a bad idea.</span></p><p class="c1 c10"><span class="c4">&gt;&gt; </span><span class="c4 c73">If</span><span class="c4">&nbsp;the implementation is easy to explain, it may be a good</span></p><p class="c1 c10"><span class="c4">&nbsp; &nbsp;idea.</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">Similarly, we can use the dollar sign &ldquo;$&rdquo; to only print lines ending with the matched pattern: </span></p><p class="c0 c9"><span class="c3"></span></p><p class="c0"><span class="c3">$ &nbsp;grep idea.$ zen.txt</span></p><p class="c0"><span class="c3">&gt;&gt; If the implementation is hard to explain, it&#39;s a bad </span><span class="c3 c73">idea</span><span class="c3">.</span></p><p class="c0"><span class="c3">&gt;&gt; If the implementation is easy to explain, it may be a good </span><span class="c3 c73">idea</span><span class="c3">.&rdquo;</span></p><p class="c0 c9"><span class="c3"></span></p><p class="c1"><span class="c3">The lines matched both end with </span><span class="c3">idea</span><span class="c3">. The line </span><span class="c4">Namespaces are one honking great idea -- let&#39;s do more of those!</span><span class="c3">&nbsp;was ignored because although it includes the word idea, it does not end with the word idea. You can also combine the two anchor matches we&rsquo;ve covered into the regular expression </span><span class="c4">^$</span><span class="c3">&nbsp;to search for empty lines in a file. </span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.3il8wpldaydb"><span>Match Between</span><sup><a href="#cmnt36" id="cmnt_ref36">[aj]</a></sup></h3><h3 class="c1 c13" id="h.regugyg5up2m"><span>Brackets</span></h3><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;You can use brackets in a regular expression to match any of the characters inside the brackets. In this example, instead of matching text from our </span><span class="c4">zen.txt</span><span class="c3">&nbsp;file, we are going to use a pipe to pass in a string to </span><span class="c4">grep</span><span class="c3">:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c4">&nbsp;$ echo Two is a number and too is not. | grep &nbsp;-i t[ow]o</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; Two is a number and too is not.</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">Remember, the pipe symbol passes the output of one command as the input of the next. In this case the output of echo is passed as the input of grep. </span><span class="c3">The command </span><span class="c4">echo Two is a number and too is not. | grep &nbsp;-i t[ow]o</span><span class="c3">&nbsp;will match both </span><span class="c4">two</span><span class="c3">&nbsp;and </span><span class="c4">too</span><span class="c3">&nbsp;because the regex is looking for a </span><span class="c4">t</span><span class="c3">&nbsp;followed by either an </span><span class="c4">o</span><span class="c3">&nbsp;or a </span><span class="c4">w</span><span class="c3">&nbsp;followed by an </span><span class="c4">o</span><span class="c3">.</span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.ndlgzvn4w8s3"><span>Repetition</span></h3><p class="c1"><span class="c3 c5">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">We can add repetition in our patterns with the </span><span class="c4">*</span><span class="c3">&nbsp;symbol. We might want to say, match </span><span class="c4">tw </span><span class="c3">followed by any amount of </span><span class="c4">o&rsquo;s</span><span class="c3">. The regular expression</span><span class="c4">&nbsp;grep two*</span><span class="c3 c5">&nbsp;</span><span class="c3">accomplishes this:</span></p><p class="c1 c9"><span class="c3 c5"></span></p><p class="c0"><span class="c4">$ echo two twooo twoo not too. | grep -o two*</span></p><p class="c0"><span class="c4">&gt;&gt; two</span></p><p class="c0"><span class="c4">&gt;&gt; twooo</span></p><p class="c0"><span class="c4">&gt;&gt; twoo</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">Adding a </span><span class="c4">*</span><span class="c3">&nbsp;after the </span><span class="c4">o</span><span class="c3">&nbsp;means the regular expression should match anything with </span><span class="c4">tw</span><span class="c3">&nbsp;followed by any number of </span><span class="c4">o&rsquo;s</span><span class="c3">. </span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.i6xixw09m45"><span>Range</span></h3><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;You can match a range of letters or numbers by putting the range inside brackets. For example, we might want to only match the numbers in </span><span class="c4">122233 hello 334443 goodbye 939333</span><span class="c3">. The regex </span><span class="c4">[0-9]*</span><span class="c3">&nbsp;will match all the numbers in the string because it includes a range of numbers (</span><span class="c4">0-9)</span><span class="c3">, followed by </span><span class="c4">*</span><span class="c3">&nbsp;which tells the regex to match as many numbers in a row as it can. </span></p><p class="c1"><span class="c3">&nbsp; &nbsp;</span></p><p class="c0"><span class="c4">$ &nbsp;echo 122233 hello 334443 goodbye 939333 | grep -0 [0-9]* </span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; 122233</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; 33443</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; 939333</span></p><p class="c1 c9"><span class="c3 c5"></span></p><p class="c1"><span class="c3">Similarly, you can match a range of characters (all characters in this case) with the rege</span><span class="c3">x [a-zA-Z]:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c0"><span class="c3">$ echo 122233 hello 334443 goodbye 939333 | grep </span><span class="c3">&ldquo;[a-zA-Z]*&rdquo;</span><sup><a href="#cmnt37" id="cmnt_ref37">[ak]</a></sup></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.xqtgwjh5ehfo"><span>Escaping &nbsp; </span></h3><p class="c1"><span class="c3 c5">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">What if we want to match one of the special characters we&rsquo;ve been discussing, like the dollar sign? We can do this by escaping the character. We covered escaping in the chapter Manipulating Strings: escaping means prefixing a character with a special character to let the program evaluating the syntax know you want to use the actual character and not the special meaning the character normally has. Escaping in regular expressions is done with a backward slash:</span></p><p class="c1 c9"><span class="c3 c5"></span></p><p class="c0"><span class="c3">$ echo I love $ | grep &nbsp;\$ </span></p><p class="c0"><span class="c3">&gt;&gt; I love $</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">Normally, the dollar sign has the special meaning of only matching something at the end of a line, however because we escaped it, our regex looks for the dollar sign. </span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.c579tvwymfvb"><span>grep -E</span></h3><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;The -ep command extends grep with even more functionality such as matching different alternatives. With the -E flag, you can use pipes to match multiple patterns:</span></p><p class="c1 c9"><span class="c3 c5"></span></p><p class="c0"><span class="c4">$ grep -E &nbsp;</span><span class="c4">&quot;</span><span class="c4">if|it&rsquo;s</span><span class="c4">&quot;</span><span class="c4">&nbsp;zen.txt </span></p><p class="c0"><span class="c4">&gt;&gt; Beaut</span><span class="c4 c73">if</span><span class="c4">ul is better than ugly.</span></p><p class="c0"><span class="c4">&gt;&gt; </span><span class="c4 c73">If</span><span class="c4">&nbsp;the implementation is hard to explain, it&#39;s a bad idea.</span></p><p class="c0 c9"><span class="c3"></span></p><p class="c1"><span class="c4">grep -E &nbsp;&quot;if|it&rsquo;s&quot; zen.txt </span><span class="c3">matches any line with either if or it&rsquo;s. </span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.v6pl4ylpwko4"><span>More Repetition</span></h3><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;You may want to match a repeating pattern, but only a limited number of times. A common example is matching a sequence of digits &nbsp;of a specific length. </span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$ </span><span class="c4">echo 123 1234 5678 22222 | grep -E -o [0-9]{4}</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; 1234</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; 5678</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; 2222</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">This regex will match all four digit numbers, ignoring 123 and any other number with less than 3 digits, while this regex will match sequences of number with &nbsp;four to six digits:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c4">$ echo 1 12 123 1234 5678 22222 123456 | grep -E -o </span></p><p class="c0"><span class="c4">&nbsp; </span><span class="c4">&quot;</span><span class="c4">[0-9]{4,6}</span><span class="c4">&quot;</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; 1234</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; 5678</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; 22222</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; 123456</span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.noleud4lhusp"><span>Regular Expressions in Python</span><sup><a href="#cmnt38" id="cmnt_ref38">[al]</a></sup></h3><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Python has a library called </span><span class="c4">re</span><span class="c3">&nbsp;that lets you use regular expressions in Python programs. </span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;import re</span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.lcaf6b8t881t"><span>Wrapping Up</span></h3><p class="c0"><span class="c3">Regular expressions are a powerful tool when used in moderation. They let you quickly create powerful matching patterns and are useful in all kinds of situations, from searching a file from the command line to searching strings for a patterns in Python. </span></p><p class="c0 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.1vf4awosh5jy"><span>Zen Challenge</span></h3><p class="c1"><span class="c3">Write a regular expression to find every word in the Zen of Python ending with the letter </span><span class="c4">y</span><span class="c3">. </span></p><p class="c1 c9"><span class="c3"></span></p><h2 class="c1 c13" id="h.4vrq1xqluo2t"><span>Package Managers </span></h2><p class="c1"><span class="c3">&ldquo;Every programmer is an author.&rdquo; </span></p><p class="c1"><span class="c3">&#8213;</span><span class="c3"><a class="c18" href="https://www.google.com/url?q=https://www.goodreads.com/author/show/7046974.Sercan_Leylek&amp;sa=D&amp;ust=1467337426941000&amp;usg=AFQjCNGyyPShVYgJNBlh7DTekaLtSW81RA">&nbsp;</a></span><span class="c17 c3"><a class="c18" href="https://www.google.com/url?q=https://www.goodreads.com/author/show/7046974.Sercan_Leylek&amp;sa=D&amp;ust=1467337426941000&amp;usg=AFQjCNGyyPShVYgJNBlh7DTekaLtSW81RA">Sercan Leylek</a></span></p><p class="c1 c9"><span class="c17 c3"><a class="c18" href="https://www.google.com/url?q=https://www.goodreads.com/author/show/7046974.Sercan_Leylek&amp;sa=D&amp;ust=1467337426942000&amp;usg=AFQjCNEexwXPRQBDoukIBioVbF3H6ea6RA"></a></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;In this chapter we are going to take a look at using package managers to install and manage programs on your computer. </span><span class="c3">In the process of creating new programs, you will often need to use other people&rsquo;s programs. For example, if you want to create a website, often times you will want to use a program called a web framework. Flask is the name of a web framework written in Python. When you are creating a website, you often have to do the same things over and over. You need to handle HTTP requests for example, which we will learn about in in Part IV of this book. Instead of writing all the code to handle HTTP requests, and other functionality yourself, you can download and install a web framework like Django. Incorporating other people&rsquo;s programs into your own work can greatly decrease the time it takes you to finish a project. There are thousands of programs people have built to solve different problems available for you to use for free, and programmers use them all the time. Many of these projects &nbsp;can be found on GitHub. &nbsp;In order to install and manage all of these programs, programmers use a program called a package manager. Most of the programs you will use are built on top of other people&#39;s programs. These are called the program&#39;s dependencies. A package manager will download a program for you, and satisfy its dependencies, so you can successfully use it in your new program. </span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Python comes with a package manager called </span><span class="c4">pip</span><span class="c3">&nbsp;that manages Python programs for you</span><span class="c3">. However, you will also want to use another package that can download programs that are not written in Python. For instance, if you are building a web application, you will most likely need a database. While you can&rsquo;t download a database with </span><span class="c4">pip</span><span class="c3">, you can easily download it with a package manager like Apt-get, which comes with Ubuntu. </span><span class="c3">In this chapter will learn how to use three package managers: HomeBrew, Apt-get and </span><span class="c4">pip</span><span class="c3">. HomeBrew is built for OS X, Apt-get comes with Ubuntu and </span><span class="c4">pip</span><span class="c3">&nbsp;comes with Python. If you don&rsquo;t have a Mac, you can skip the HomeBrew section, but no matter what operating system you use, learning to use Apt-get will be useful because Linux is so prevalent in the programming world. </span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.q6sudd7keew"><span>Apt-get</span></h3><p class="c1"><span class="c3 c5">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">Apt-get is a package manager that comes with Ubuntu. To try it out, go to &nbsp;your VirtualBox installation of Ubuntu, and open up the terminal. You can install a package with </span><span class="c4">apt-get install [package_name]</span><span class="c3">. You need to use apt-get as the root user, so the command becomes </span><span class="c4">sudo apt-get install [package_name]</span><span class="c3">. Install the package </span><span class="c4">aptitude</span><span class="c3">:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c0"><span class="c4">$ sudo apt-get install aptitude</span></p><p class="c0 c9"><span class="c3"></span></p><p class="c1"><span class="c3">Once the package is successfully installed, try running it:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$ aptitude </span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; Do you want to continue? &nbsp;[Y/N]Y</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&hellip;</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; Processing triggers for libc-bin (2.19-0ubuntu6.7) &hellip;</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt;aptitude</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">This will open up Aptitude, a helpful program that shows information about packages. You should see a list of all the packages installed on your operating system, as well as a list of available packages you&rsquo;ve yet to install. You can uninstall aptitude with </span><span class="c4">sudo apt-get uninstall aptitude</span><span class="c3">. That&rsquo;s all there is to it. Installing and uninstalling programs is as simple as using two commands with </span><span class="c4">apt-get</span><span class="c3">. </span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.kd0r6hj5hd44"><span>pip</span></h3><p class="c1"><span class="c3 c5">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">When you create software, there are several different types of programs you can make. For example, you can create an application, a library or a framework. An application is a consumer facing program, like an app. A library is software meant to be used by other programmers, like Python&rsquo;s well known request library that makes it easy to work with HTTP, among other things. A framework is also software designed for other programmers to use. Frameworks solve problems for a specific problem domain, the user of the framework then writes code that the framework calls.</span></p><p class="c0"><span class="c3">&nbsp;As we discussed earlier, when you write a program, you should use other people&rsquo;s libraries and frameworks when you can, and you can easily download libraries and frameworks with </span><span class="c4">pip</span><span class="c3">. To use </span><span class="c4">pip</span><span class="c3">&nbsp;go to the</span><span class="c3">&nbsp;command line</span><span class="c3">&nbsp;and type </span><span class="c4">pip</span><span class="c3">, to see a list of commands you can use. &nbsp;For example, we can use </span><span class="c4">pip install </span><span class="c3">to install a new program. Type:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c0"><span class="c3">&nbsp;</span><span class="c4">pip install flask</span><sup><a href="#cmnt39" id="cmnt_ref39">[am]</a></sup><span class="c3">&nbsp;</span></p><p class="c0 c9"><span class="c3"></span></p><p class="c1"><span class="c3">This will &nbsp;install the Python web framework Flask. Now, if you write a program, you will be able to import the Flask module. Open up a new Python file and type the code:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c10"><span class="c7 c4 c5">from </span><span class="c4 c28">flask </span><span class="c7 c4 c5">import </span><span class="c4 c28">Flask</span></p><p class="c1 c9 c10"><span class="c4 c28"></span></p><p class="c1 c10"><span class="c4 c28">app = Flask(__name__)</span></p><p class="c1 c9 c10"><span class="c4 c28"></span></p><p class="c1 c9 c10"><span class="c4 c28"></span></p><p class="c1 c10"><span class="c4 c28">@</span><span class="c54 c4">app.route</span><span class="c4 c28">(</span><span class="c40 c4">&#39;/&#39;</span><span class="c4 c28">)</span></p><p class="c1 c10"><span class="c7 c4 c5">def </span><span class="c4 c28 c5">index</span><span class="c4 c28">():</span></p><p class="c1 c10"><span class="c4 c28">&nbsp; &nbsp;</span><span class="c7 c4 c5">return </span><span class="c40 c4">&quot;Hello, World!&quot;</span></p><p class="c1 c9 c10"><span class="c40 c4"></span></p><p class="c1 c10"><span class="c4 c28">app.run(</span><span class="c4 c63">port</span><span class="c4 c28">=</span><span class="c40 c4">&#39;8000&#39;</span><span class="c4 c28">)</span></p><p class="c1 c9 c10"><span class="c3"></span></p><p class="c1"><span class="c3">If you go to </span><span class="c4">http://127.0.0.1:8000/</span><span class="c3">&nbsp;in your web browser, you will see a website that says &ldquo;Hello, World!&rdquo;. We just downloaded and used the Flask web framework to create a webpage in a matter of minutes. </span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">The command </span><span class="c4">pip freeze</span><span class="c3">&nbsp;shows you all of the programs you&rsquo;ve downloaded with pip. </span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c4">pip freeze</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; Flask==0.10.1</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">You can also use</span><span class="c4">&nbsp;pip freeze &gt; [filename]</span><span class="c3">&nbsp;to save your program&rsquo;s </span><span class="c3">dependencies</span><span class="c3">&nbsp;to a file. This will create a text file with all the modules your project depends on. Create a requirements file with:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c4">pip freeze &gt; requirements.txt</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt;</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">You now have &nbsp;a requirements file with all of the programs you&rsquo;ve installed with </span><span class="c4">pip</span><span class="c3">. Open </span><span class="c4">requirements.txt</span><span class="c3">&nbsp;with a text editor to see the new file. This is helpful, because you can also pass a requirements file into the </span><span class="c4">pip install</span><span class="c3">&nbsp;command, and it will &nbsp;install everything in the requirements file for you e.g., </span><span class="c4">pip install requirements.txt.</span><span class="c3">&nbsp;If you need to resolve all the dependencies of a project, you can quickly do it with this command. You may be wondering &ldquo;Why would I ever have to resolve the dependencies of a program myself if pip does it for me?&rdquo;. Well, in order for a program to be available on pip, the person who wrote it has to upload it to the website </span><span class="c17 c3"><a class="c18" href="https://www.google.com/url?q=https://pypi.python.org/pypi&amp;sa=D&amp;ust=1467337426968000&amp;usg=AFQjCNGjZxvRCsOM1KDGVrsdDolrYKo6vA">https://pypi.python.org/pypi</a></span><span class="c3">, and you may be working on a program that hasn&rsquo;t been uploaded yet. </span></p><hr style="page-break-before:always;display:none;"><p class="c1 c9"><span class="c3"></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">Finally, you can uninstall programs you&rsquo;ve downloaded with </span><span class="c4">pip uninstall</span><span class="c3">. To uninstall Flask, use the command</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c0"><span class="c3">&nbsp;</span><span class="c4">pip uninstall flask</span><span class="c3">.</span></p><p class="c0"><span class="c3">&hellip;</span></p><p class="c0"><span class="c3">&gt;&gt; Proceed (y/n)? y</span></p><p class="c0"><span class="c3">...</span></p><h3 class="c1 c13" id="h.eh79ntd33rdw"><span>Homebrew</span></h3><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Homebrew is one of the most popular package managers for OS X. If you have a Mac, you can install Homebrew onto your machine from the command line. Go to </span><span class="c17 c3"><a class="c18" href="https://www.google.com/url?q=http://brew.sh/&amp;sa=D&amp;ust=1467337426972000&amp;usg=AFQjCNHM30mEjZ8JxRUcxXrnkZoKm3SpJA">http://brew.sh/</a></span><span class="c3">&nbsp;and copy and paste the provided script in the </span><span class="c3">terminal</span><sup><a href="#cmnt40" id="cmnt_ref40">[an]</a></sup><span class="c3">. &nbsp;If the installation worked, you should see a list of commands when you type </span><span class="c4">brew</span><span class="c3">&nbsp;into your terminal:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c4">$ brew</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; Example usage:</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; &nbsp;brew [info | home | options ] [FORMULA...]</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">There are two commands you will use most frequently:install and uninstall. We can install a calculator program called calc from the terminal with the install command:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c0"><span class="c4">$ brew install calc</span></p><p class="c0"><span class="c4">...</span></p><p class="c0"><span class="c4">&gt;&gt; ==&gt; Pouring calc-2.12.5.0.el_capitan.bottle.tar.gz</span></p><p class="c0"><span class="c4">&gt;&gt; &#x1f37a; &nbsp;/usr/local/Cellar/calc/2.12.5.0: 518 files, 4.4M</span></p><p class="c0 c9"><span class="c3"></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">That&rsquo;s all there is to it, calc is now installed.You can play around with it by typing calc from the command line (type quit to exit); </span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; 2 + 2</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; 4</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">The command</span><span class="c4">&nbsp;brew list</span><span class="c3">&nbsp;lists the software you&rsquo;ve installed using Homebrew:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$ brew list</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; calc</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">If you want, you can uninstall calc &nbsp;with the command </span><span class="c4">brew uninstall calc</span><span class="c3">. That is all you need to know. You are ready to start using HomeBrew to manage your packages. </span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.ocl7p9k386su"><span>Wrapping Up</span></h3><p class="c0"><span class="c3">Using other people&rsquo;s software will speed up the development of your programs because you won&rsquo;t waste time solving problems that have already been solved. </span><span class="c3">Package managers take all the work out of installing other people&rsquo;s software, so make sure to use them. </span></p><p class="c0 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.hptg87me05cy"><span>New Software Challenge</span></h3><p class="c1"><span class="c3 c5">F</span><span class="c3">ind three programs that interest you on Ubuntu using aptitude and install them using apt-get. </span></p><p class="c1 c9"><span class="c3"></span></p><h2 class="c1 c13" id="h.9nrq8rr7ac2z"><span>Version Control</span></h2><p class="c1"><span class="c3">I object to doing things that computers can do.</span></p><p class="c1"><span class="c3">&mdash; Olin Shivers</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Writing software is a team sport. Most of the time you will not be writing software alone, you will be collaborating with other people using version control systems - software designed to let you easily work on projects with other programmers. When you are working on a project with more than one person, you will both be making changes to the codebase, and you need to keep those changes in sync. You could both email each other with your changes, and combine the two versions yourself, but that would quickly become tedious. Also what would happen if you both made changes to the same part of the project? Whose changes should be used? These are the kinds of problems version control solves.</span></p><p class="c0"><span class="c3">There are many different version control systems, with Git and SVN being the two most popular. In this chapter we are going to put a project on Github, which you can think of as the place you store your code on the cloud, using Git, a popular program that manages all of your version control needs.</span></p><h3 class="c1 c13" id="h.41asqaaoe1ax"><span>Getting Started</span></h3><p class="c1"><span class="c3">To get started, you need to create a Github account. Go to Github.com/join to create a Gitub account. You are also going to need to install Git. You can install Git using either Homebrew or Apt-get with &ldquo;brew install git&rdquo; or &ldquo;apt-get git&rdquo; respectively. &nbsp;If you are Windows, you can download an installer to download Git, google &ldquo;install windows Git&rdquo; to find the latest one. &nbsp; </span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">Once you&rsquo;ve downloaded Git</span><sup><a href="#cmnt41" id="cmnt_ref41">[ao]</a></sup><span class="c3">, Create and enter a new folder called &ldquo;my_project&rdquo; anywhere on your computer, and create a file called &ldquo;hello_world.py&rdquo; with &ldquo;print &lsquo;hello world&rsquo; in it.</span></p><p class="c0"><span class="c3">Now we are going to create a new repository on Github. Login to your GitHub account at github.com and click on the &ldquo;+&rdquo; button at the top right corner of the screen. Click &ldquo;Create repository&rdquo; from the dropdown menu. Give the repository the name &ldquo;my_project&rdquo;. Make it public, and check &ldquo;initialize the repository with a readme&rdquo;. Now click &ldquo;Create a repository&rdquo;. We just created our first Git repository. </span></p><h3 class="c1 c13" id="h.9z1ggmn0ic0"><span>Repositories</span></h3><p class="c1"><span class="c3">Readwrite.com defines a repository as &ldquo;a digital directory or storage space where you can access your project, its files, and all the versions of its files that Git saves.&rdquo; Each programming project you work on will have its own repository, or &ldquo;repo&rdquo; as the cool kids say. </span></p><p class="c1"><span class="c3"># explain origin master</span></p><h3 class="c1 c13" id="h.hm3zvtkhbwxp"><span>Pushing </span></h3><p class="c1"><span class="c3">At the highest level, there are two main things you will be doing with Git. The first is pushing data from your project to your repository. An example of this would be if you were working with someone else on a project and you made some changes. You want to update your repository so that your partner can use the new changes. To do this, you push your data to your Github repository. &nbsp;</span></p><h3 class="c1 c13" id="h.74qai0k92i57"><span>Pulling</span></h3><p class="c1"><span class="c3">Once the repository is up to date with your new changes, your partner can download them. He does this by pulling data from Github. You can pull data from Github at any time, and Git will update your project with all the latest changes. </span></p><h3 class="c1 c13" id="h.v1yveyvzylas"><span>Syncing our repository</span></h3><p class="c1"><span class="c3">Back to our project. Our project consists of a program that prints &ldquo;Hello World&rdquo;, but it&#39;s not using version control yet. Lets fix that. Go to the terminal and make sure you are in the directory &ldquo;my_git_project&rdquo;. Use the command:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c0"><span class="c3">&nbsp;$ &ldquo;git init&rdquo;</span></p><p class="c0"><span class="c3">&gt;&gt; Initialized empty Git repository in [folder] </span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">Git init initializes a new Git repository. This means that the project we created can now be managed using Git. It is not enough to simply initialize a project. Your project needs to be linked to a repository on Github. The command &ldquo;git remote -v&rdquo; shows what repository your project is linked to. When you are using programs from the command line, adding &ldquo;-v&rdquo; typically stands for verbose, which means you will get more information from your command. Enter that command:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c10"><span class="c3">$ git remote -v</span></p><p class="c1 c10"><span class="c3">&gt;&gt; </span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;Nothing will show up because our project is not linked to a repository on Github. Let&#39;s change that. Enter the command &ldquo;git remote add origin </span><span class="c17 c3"><a class="c18" href="">https://github.com/[your_username]/my_project.git</a></span><span class="c3">&rdquo;:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$ git remote add origin </span><span class="c17 c3"><a class="c18" href="https://www.google.com/url?q=https://github.com/calthoff/my_git_project.git&amp;sa=D&amp;ust=1467337426992000&amp;usg=AFQjCNFC2CYaU0SaZUPWCJs9yeyf1MGQrg">https://github.com/calthoff/my_project.git</a></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; </span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;Enter the command &ldquo;git remote -v&rdquo;:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$ git remote -v</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; origin https://github.com/calthoff/my_git_project.git (fetch)</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; origin https://github.com/calthoff/my_git_project.git (push)</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">Early I explained there are two high level things you do with git- pushing and fetching. The first line shows the repository your project will fetch data from, and the second line shows the repository your project will push data to. In most cases, you will push and pull from the same repository. </span></p><h3 class="c1 c13" id="h.aqc0y49pmus9"><span>Pulling Example</span></h3><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;When we created our repository, we selected the option to create a readme file. When our repository was created, Github automatically added an empty readme file to our repository. You can see this by looking at you repository on Github.com. Our local project on our computer doesn&rsquo;t have a readme file. That means our project and our repository are out of sync. We can fix that by fetching data from Github. Go to the terminal, make sure you are in your my_git_project directory and enter:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c0"><span class="c3">$ git pull origin master</span></p><p class="c0"><span class="c3">&gt;&gt;From https://github.com/calthoff/my_project</span></p><p class="c0"><span class="c3">&gt;&gt; * branch &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;master &nbsp; &nbsp; -&gt; FETCH_HEAD</span></p><p class="c0 c9"><span class="c3"></span></p><p class="c1"><span class="c3">The readme file has been added to your project folder:</span></p><p class="c1"><span class="c3">&nbsp;</span></p><p class="c0"><span class="c3">$ ls</span></p><p class="c0"><span class="c3">&gt;&gt; README.md&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hello_world.py</span></p><p class="c0 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.rvynwx6ci41t"><span>Pushing Example</span></h3><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Our project now has the readme file from our Github repository. However, our project has a Python file called &ldquo;hello_world.py&rdquo; that is not in our repository on Github. Our project is out of sync with our repository. We can fix this by pushing our changes to our repository. </span></p><p class="c1"><span class="c3">There are three things we need to do to push our data to our repo. I usually start the process with the command &ldquo;git status&rdquo;. Enter git status in the terminal inside of your my_git_project directory:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$ git status</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; On branch master</span></p><p class="c1 c10"><span class="c3">Untracked files:</span></p><p class="c1 c10"><span class="c3">&nbsp; (use &quot;git add &lt;file&gt;...&quot; to include in what will be committed)</span></p><p class="c1 c9 c10"><span class="c3"></span></p><p class="c1 c10"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3 c73">hello_world.py</span></p><p class="c1 c9 c10"><span class="c3"></span></p><p class="c1 c10"><span class="c3">nothing added to commit but untracked files present (use &quot;git add&quot; to track)</span></p><p class="c1 c9 c10"><span class="c3"></span></p><p class="c1"><span class="c3">&ldquo;git status&rdquo; will show you the current state of your project in relation to your repository. New files that exist in your project, but do not exist in your repository are shown in green, as are deleted files. Files that have been modified are in red. A file is modified if it is differs from your repository. This is our chance to decide the changes we want to push to our repository. </span></p><p class="c0"><span class="c3">The next step is the &ldquo;staging phase&rdquo;. The staging phase is the phase that happens once we&rsquo;ve decided all of the change we want to push to our repository. We &ldquo;stage&rdquo; a file with the add command. Use the command &ldquo;git add hello_world.py&rdquo; to stage our Python file:</span></p><p class="c0 c9"><span class="c3"></span></p><p class="c0"><span class="c3">$ git add hello_world.py</span></p><p class="c0"><span class="c3">&gt;&gt;</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c0"><span class="c3">Enter &ldquo;git status&rdquo; again and you should now see my_project.py in green.:</span></p><p class="c0 c9"><span class="c3"></span></p><p class="c0"><span class="c3">$ git status</span></p><p class="c0"><span class="c3">&gt;&gt; On branch master</span></p><p class="c0"><span class="c3">Changes to be committed:</span></p><p class="c0"><span class="c3">&nbsp; (use &quot;git reset HEAD &lt;file&gt;...&quot; to unstage)</span></p><p class="c0 c9"><span class="c3"></span></p><p class="c0"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3 c66">new file: &nbsp; hello_world.py</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">If you stage a file and change your mind like we just did, you can undo it without making any changes to your repository. This is important because your repository has a history it keeps track of, and you want to avoid unnecessary pushes to your repo to avoid cluttering this history. Try unstagging hello_wold.py with &ldquo;git reset hello_world.py&rdquo; and adding it again with &ldquo;git add my_project.py&rdquo;</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Once we&rsquo;ve staged our files, we need to &ldquo;commit&rdquo; them:</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$ git commit -m &ldquo;my first commit&rdquo;</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; &nbsp;1 file changed, 1 insertion(+)</span></p><p class="c1 c23"><span class="c3">&nbsp;create mode 100644 hello_world.py</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">When you commit files, you want to use the -m flag so you can pass along a message. This message will be saved along with your commit to help you remember what changes you made to your repository. </span></p><p class="c1 c9"><span class="c3"></span></p><p class="c0"><span class="c3">The final step is to push your changes to Github. We this with the command &ldquo;git push origin master&rdquo;. &nbsp;</span></p><p class="c0 c9"><span class="c3"></span></p><p class="c0"><span class="c3">$ git push origin master</span></p><p class="c0"><span class="c3">&gt;&gt; Counting objects: 3, done.</span></p><p class="c0"><span class="c3">Delta compression using up to 4 threads.</span></p><p class="c0"><span class="c3">Compressing objects: 100% (2/2), done.</span></p><p class="c0"><span class="c3">Writing objects: 100% (3/3), 309 bytes | 0 bytes/s, done.</span></p><p class="c0"><span class="c3">Total 3 (delta 0), reused 0 (delta 0)</span></p><p class="c0"><span class="c3">To https://github.com/calthoff/my_project.git</span></p><p class="c0"><span class="c3">&nbsp; &nbsp;0eb3a47..48acc38 &nbsp;master -&gt; master</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">Origin refers to the name of your repository, which is origin by default, and master refers to the master branch, which we cover in the section branching. Once you enter your Github username and password from the command line, your changes will be pushed to Github. If you look at your repository on Github&rsquo;s website, you will see my_project.py is now included in your project. </span></p><h3 class="c1 c13" id="h.84tcmxa0x1np"><span>Reverting Versions</span></h3><p class="c1"><span class="c3 c5">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">With version control, the entire history of your project is saved and available for you to use. If you decide you want to revert to a commit from 10 days ago, you are able to do so. &nbsp;You can see your project&rsquo;s history of commits with &ldquo;git log&rdquo;, which should output something like this:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$ git log</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;commit aeb4ef3cf3aabdb9205ea9e96e8cab5c0f5ca7ea</span></p><p class="c1 c10"><span class="c3">Author: Cory Althoff &lt;coryalthoff@Corys-MacBook-Pro.local&gt;</span></p><p class="c1 c10"><span class="c3">Date: &nbsp; Thu Jan 21 13:52:02 2016 -0800</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">The string of numbers and letters after &ldquo;commit&rdquo; is the commit number. We can use this number to revert our project to exactly how it was at that time. &nbsp;We can travel back in time with the command git checkout [old commit]. In this case the command would be &ldquo;git checkout aeb4ef3cf3aabdb9205ea9e96e8cab5c0f5ca7ea&rdquo;.</span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.6sx1o0qlabk5"><span>d</span><span>iff</span></h3><p class="c1"><span class="c3">W</span><span class="c3">e can use the command &ldquo;git diff&rdquo; to see the difference between the version of a file in our local project, and the version in our repo. Add &nbsp;&ldquo;x=100&rdquo; to our &ldquo;hello_world.py&rdquo; file. Enter the command:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c0"><span class="c3">$ &nbsp;git diff hello_world.py</span></p><p class="c0"><span class="c3">&gt;&gt; </span><span class="c3 c5">diff --git a/hello_world.py b/hello_world.py</span></p><p class="c0"><span class="c3 c5">index b376c99..83f9007 100644</span></p><p class="c0"><span class="c3 c5">--- a/hello_world.py</span></p><p class="c0"><span class="c3 c5">+++ b/hello_world.py</span></p><p class="c0"><span class="c3">@@ -1 +1,2 @@</span></p><p class="c0"><span class="c3">&nbsp;print(&#39;hello&#39;)</span></p><p class="c0"><span class="c3 c58">+x = 100</span></p><p class="c0 c9"><span class="c3"></span></p><p class="c1"><span class="c3">Git highlights &ldquo;+x=100&rdquo; in green because the line changed, the &ldquo;+&rdquo; is to signify the line &nbsp;&ldquo;x=100&rdquo; was added.</span></p><h3 class="c1 c13" id="h.erd2oymrigp"><span>Branching</span></h3><p class="c1"><span class="c3 c5">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">Branching is a feature of Git that allows you to switch back and forth between different versions of your project. Create a new branch with:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c0"><span class="c3">&nbsp;$ git checkout -b v1</span></p><p class="c0"><span class="c3">&nbsp;&gt;&gt; Switched to a new branch &#39;v1&#39;</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">Create a new file called &ldquo;v1.py&rdquo; and add the code &ldquo;print(&ldquo;v1&rdquo;)&rdquo; to it. Run the new program with:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$ python v1.py</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; v1</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">Now create another branch with:</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$ git checkout -b v2</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; Switched to a new branch &#39;v2&#39;</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">Create a new file called &ldquo;v2.py&rdquo; and add the code &ldquo;print(&ldquo;v2&rdquo;) to it. Run the new program with:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$ python v2.py</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; v2</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">Now you can switch bath and forth between these two branches:</span></p><p class="c0"><span class="c3">$ git checkout v1</span></p><p class="c0"><span class="c3">&gt;&gt; Switched to a new branch &#39;v1&#39;</span></p><p class="c0 c9"><span class="c3"></span></p><p class="c0"><span class="c3">$ git checkout v2</span></p><p class="c0"><span class="c3">&gt;&gt; &nbsp;Switched to a new branch &#39;v1&#39;</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">Try switching back and forth between the two branches and inspecting the code. As you can see, we now have two versions of the same project we can switch back and forth between. </span></p><p class="c0"><span class="c3">The the command &ldquo;git branch&rdquo; lists all of your branches and denotes the branch you are on with an asterisk:</span></p><p class="c0 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$ git branch</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; master</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; *v1</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; v2</span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.ftkq3fo8s2n3"><span>Merging</span></h3><p class="c0"><span class="c11 c3">You typically create a new branch when you want to develop a new feature for your program or fix a bug or problem. When you are finished with whatever you are doing on your branch, you normally &ldquo;merge&rdquo; your changes with the master branch, which is the process of combining the two branches. Let&rsquo;s merge our v1 branch with our master branch:</span></p><p class="c0 c9"><span class="c11 c3"></span></p><p class="c0"><span class="c11 c3">$ git checkout master</span></p><p class="c0"><span class="c11 c3">$</span></p><p class="c1 c9"><span class="c11 c3"></span></p><h3 class="c1 c13" id="h.d0uu1277h4ui"><span>The Other Pull Request</span></h3><p class="c1"><span class="c3 c5">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">Confusingly, there are two concepts named &ldquo;pull&rdquo; in version control. We previously talked about pulling data from your repository. There is also an unrelated concept called a pull request. A pull request takes place on your repository hosting service, in our case, Github. If you are working on a branch of a project, and you want to merge it with the master repository, you would issue a pull request to merge the two. This gives your teammates the chance to review and comment on your changes. If everything looks good, someone on your team can approve the pull request and merge the two branches. </span></p><h3 class="c1 c13" id="h.k8ihvo45m8ho"><span>Clone a Project</span></h3><p class="c1"><span class="c3">Go to your repository on GitHub, and you will see &ldquo;HTTPS:&rdquo; followed by a link. You can use this link to download your project, and initialize it as a Git repository, in a different location on your computer or on another computer altogether. You just have to copy the link, or press the copy link to clipboard button, and use it with the command &ldquo;git clone&rdquo;, i.e.. &ldquo;git clone [repository_url]&rdquo;. Git will download a repository for you in whatever folder you issue the command from. The repository will already be initialized, so you can immediately run commands like &ldquo;git status&rdquo; in it. </span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.vytqmc4n1b0p"><span>Wrapping Up</span></h3><p class="c1 c9"><span></span></p><h3 class="c1 c13" id="h.vhytjap0j9ny"><span>Challenge</span><sup><a href="#cmnt42" id="cmnt_ref42">[ap]</a></sup></h3><p class="c1 c9"><span></span></p><h2 class="c1 c13" id="h.ldebqocqaxgk"><span>Using an IDE</span></h2><p class="c1"><span class="c3">If you want to go somewhere, goto is the best way to get there.</span></p><p class="c1"><span class="c3">&mdash; Ken Thompson</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c0"><span class="c3">IDE stands for Interactive Development Environment. Unless you decide to go old school, (Zed Shaw of </span><span class="c15 c3">Learn Python the Hard Way</span><span class="c3">&nbsp;think IDES make you stupid and thinks you should program in a text editor) this is where you will be writing your code. Needless to say, you will be spending a lot of time in your IDE. In this chapter I will be going over some of the features found in JetBrains IDE&rsquo;s I use to drastically increase my productivity. JetBrains makes IDES for a range of programming languages, including Python, JavaScript, Java,Ruby, C, C++, and PHP. Even if you know you are going to use a different IDE, this chapter is still worth reading because you need to have an idea of what an IDE is capable of so that you won&rsquo;t waste time doing things manually that you can quickly do with an IDE.</span></p><h3 class="c1 c13" id="h.i2cna09s2ut"><span>Jump to a Definition &nbsp;</span></h3><p class="c1"><span class="c3 c5">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">If you see a variable, function or object being used and you would like to see its definition, your IDE should have a shortcut to jump to the place in the code that defines it. You should also check to see if your IDE has a shortcut to jump back to the page you started from.</span></p><h3 class="c1 c13" id="h.izg9vkebcp1u"><span>Jump to a Line</span></h3><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Your IDE should have a command to jump to a specific line of code. If your program throws an exception, your compiler will give you the line number the exception occurred on. Use this shortcut to quickly jump to that line. </span></p><h3 class="c1 c13" id="h.ogdyohlqke3d"><span>Save Local History</span></h3><p class="c1"><span class="c3 c5">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">The save local history feature built into JetBrains IDES dramatically improved my productivity. A JetBrains IDE will automatically save a version of your project when it changes. This means you can go back and forth in time in your project. Before I knew about this feature, I would often solve a problem, change the solution, and then decide I wanted to go back to the original solution. At this point the original solution was long gone, and I would have to rewrite it again. With this feature, you can simply jump back in time 10 minutes, and reload your project exactly how it was. If you change your mind again, you can jump back and forth between the different solutions as many times as you want. </span></p><h3 class="c1 c13" id="h.s5pn9dd0s1tq"><span>Move Code Up &amp; Down</span></h3><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;In your workflow you are probably copying and pasting code a lot, moving it from one location on a page to another. Invest in learning your IDE&rsquo;s shortcut to move code up and down on the page you are on. </span></p><h3 class="c1 c13" id="h.8ldicw9l3o4z"><span>Version Control</span></h3><p class="c1"><span class="c3 c5">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">JetBrains IDES are integrated with popular version control systems like Git and SVN. Instead of having to go to the command line to push your latest, you can do it from your IDE with a helpful GUI. The less trips you have to make back and forth between your IDE and the command line, the more productive you will be.</span></p><h3 class="c1 c13" id="h.wzazslu75quq"><span>Command Line</span></h3><p class="c1"><span class="c3 c5">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">Speaking of which, JetBrains also has a built in command line.</span></p><h3 class="c1 c13" id="h.sw1is7o193jb"><span>Database Access</span></h3><p class="c1"><span class="c3 c5">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">JetbBrains IDEs also have a built in tool for connecting to a few relational databases, such as MySQL. If you are using a relational database, this is a huge time saver. Remembering SQL syntax is difficult, having autocomplete for your SQL is a game changer.</span></p><h3 class="c1 c13" id="h.o01ia077le9w"><span>Debugging</span></h3><p class="c1"><span class="c3 c5">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">One of the most important things you will do from your IDE is debugging. In this section I will go over some of the basics of debugging. Here is the code we will be debugging in this section:</span></p><ol class="c45 lst-kix_1zujz7wm8wi3-0 start" start="1"><li class="c1 c10 c38"><span class="c3">print &ldquo;Hello World&rdquo;</span></li><li class="c1 c9 c10 c38"><span class="c3"></span></li><li class="c1 c10 c38"><span class="c3">x = 100</span></li><li class="c1 c9 c10 c38"><span class="c3"></span></li><li class="c1 c10 c38"><span class="c3">buildObject()</span></li><li class="c1 c9 c10 c38"><span class="c3"></span></li><li class="c1 c10 c38"><span class="c3">print &ldquo;Hello!&rdquo;</span></li><li class="c1 c9 c10 c38"><span class="c3"></span></li></ol><p class="c1"><span class="c3">Debugging starts with setting a breakpoint on a specific line of your code. A breakpoint freezes the state of the program at the line of code you set your breakpoint at. Once you set your breakpoint, you run your debugger, and your program will stop execution at that breakpoint. Once your debugger stops at your breakpoint, you can start debugging. </span></p><p class="c1"><span class="c3">You have access to all of the code that executed before your breakpoint. If we set a breakpoint at line 3 in our example and run our debugger, we could use our debugger to check the value of x to make sure it&rsquo;s the value we expect it to be. </span></p><p class="c1"><span class="c3">A debugger generally has the options, step over, step into, and step out. These are used for stepping through programs. Stepping through a program means starting at the breakpoint you set, and moving line by line through the program. In our example, step over will stop execution at every line of the program. Step into will behave the same except for one difference. If there is a line with code defined somewhere else, it will jump to that code. In our example above, if we set a breakpoint at line 1 and step through the program using step into, the debugger will jump to the definition of buildObject on line 3. When you step into a function definition like buildObject, you can return back to your original starting place with step out. &nbsp;</span></p><p class="c1"><span class="c3 c5">Debugging Example</span></p><p class="c1"><span class="c3">Lets use Google DevTools to debug an actual program. Copy and paste the following code into a file called self_taught.html:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c10"><span class="c3">&lt;!DOCTYPE html&gt;</span></p><p class="c1 c10"><span class="c3">&lt;html lang=&quot;en&quot;&gt;</span></p><p class="c1 c10"><span class="c3">&lt;head&gt;</span></p><p class="c1 c10"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;meta charset=&quot;UTF-8&quot;&gt;</span></p><p class="c1 c10"><span class="c3">&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;title&gt;The Self-taught Programmer&lt;/title&gt;</span></p><p class="c1 c10"><span class="c3">&lt;/head&gt;</span></p><p class="c1 c10"><span class="c3">&lt;body&gt;</span></p><p class="c1 c10"><span class="c3">&lt;script&gt;</span></p><p class="c1 c10"><span class="c3">&nbsp; function middle(){</span></p><p class="c1 c10"><span class="c3">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;console.log(&quot;Middle!&quot;)</span></p><p class="c1 c10"><span class="c3">&nbsp; &nbsp;}</span></p><p class="c1 c10"><span class="c3">&nbsp; &nbsp;console.log(&quot;Beginning&quot;);</span></p><p class="c1 c10"><span class="c3">&nbsp; &nbsp;middle();</span></p><p class="c1 c10"><span class="c3">&nbsp; &nbsp;console.log(&quot;End&quot;)</span></p><p class="c1 c10"><span class="c3">&lt;/script&gt;</span></p><p class="c1 c10"><span class="c3">&lt;/body&gt;</span></p><p class="c1 c10"><span class="c3">&lt;/html&gt;</span></p><p class="c1 c9 c10"><span class="c3"></span></p><p class="c1"><span class="c3">Open the file in your browser. In your browser, on the page you opened, click &ldquo;control&rdquo; and in the popup menu click &ldquo;Inspect&rdquo;. Click on the &ldquo;Sources&rdquo; tab. On the very most left left side, find and click on the file self_taught.html. Once you click on it, you will see our html file in the middle column. You will also notice each line of code is numbered. Click directly on the number 12. This is how we set a breakpoint. A blue box should appear around the number. </span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Now we are ready to step through the file. Press refresh on your browser, and the program execution will stop at line 12. The very rightmost column contains the buttons to step through our code. Look for three buttons with arrows in them. Drag your mouse over each to see their names. Click on &ldquo;Step over next function call&rdquo; to move to line 13. Keep pressing it to move all the way through the program. </span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Refresh the page again. Step over one time. Now press the button &ldquo;Step into next function call&rdquo;. This takes us into our function called middle. Find the button &ldquo;Step out of current function&rdquo; and press it. That jumped us out of the middle function and to the next line in the program.&rsquo;</span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.hhmrygw2rfca"><span>Wrapping Up</span></h3><p class="c1 c9"><span></span></p><h3 class="c1 c13" id="h.uq77h4glim8v"><span>Challenge</span><sup><a href="#cmnt43" id="cmnt_ref43">[aq]</a></sup></h3><p class="c1 c9"><span></span></p><p class="c1 c9"><span></span></p><h2 class="c1 c13" id="h.fo49rwy3ss9h"><span>Let&rsquo;s Read Some Code</span></h2><p class="c1 c9"><span></span></p><p class="c1"><span class="c4">&ldquo;&ldquo;&ldquo;https://github.com/calthoff/tstp/blob/master/part_III/lets_read_some_code/lets_read_some_code.py&rdquo;&rdquo;&rdquo;</span></p><p class="c1 c9"><span></span></p><p class="c1"><span class="c7 c4 c5">import </span><span class="c4 c28">re</span></p><p class="c1 c9"><span class="c4 c28"></span></p><p class="c1 c9"><span class="c4 c28"></span></p><p class="c1"><span class="c4 c28">text = </span><span class="c40 c4">&quot;&quot;&quot;</span></p><p class="c1"><span class="c40 c4">Giraffes have aroused the curiosity of __PLURAL_NOUN__ since earliest times. The giraffe is the tallest of all living __PLURAL_NOUN__, but scientists are unable to explain how it got its long __PART_OF_THE_BODY__. The giraffe&#39;s tremendous height, which might reach __NUMBER__ __PLURAL_NOUN__, comes from its legs and __bodypart__.</span></p><p class="c1"><span class="c40 c4">&quot;&quot;&quot;</span></p><p class="c1 c9"><span class="c40 c4"></span></p><p class="c1 c9"><span class="c40 c4"></span></p><p class="c1"><span class="c7 c4 c5">def </span><span class="c4 c28 c5">mad_libs</span><span class="c4 c28">(mls):</span></p><p class="c1"><span class="c4 c28">&nbsp; &nbsp;</span><span class="c15 c4 c30">&quot;&quot;&quot;</span></p><p class="c1"><span class="c15 c4 c30">&nbsp; &nbsp;</span><span class="c15 c4 c30 c5">:param</span><span class="c15 c4 c30">&nbsp;mls: String with parts the user should fill out surrounded &nbsp;</span></p><p class="c1"><span class="c15 c4 c30">&nbsp; &nbsp;by double underscores. Underscores cannot</span></p><p class="c1"><span class="c15 c4 c30">&nbsp; &nbsp;be inside hint e.g., no __hint_hint__ only __hint__.</span></p><p class="c1"><span class="c15 c4 c30">&nbsp; &nbsp;&quot;&quot;&quot;</span></p><p class="c1"><span class="c15 c4 c30">&nbsp; &nbsp;</span><span class="c4 c28">hints = re.findall(</span><span class="c40 c4">&quot;__.*?__&quot;</span><span class="c7 c4">, </span><span class="c4 c28">mls)</span></p><p class="c1"><span class="c4 c28">&nbsp; &nbsp;</span><span class="c7 c4 c5">if </span><span class="c4 c28">hints:</span></p><p class="c1"><span class="c4 c28">&nbsp; &nbsp; &nbsp; &nbsp;new_words = []</span></p><p class="c1"><span class="c4 c28">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c7 c4 c5">for </span><span class="c4 c28">word </span><span class="c7 c4 c5">in </span><span class="c4 c28">hints:</span></p><p class="c1"><span class="c4 c28">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;new_word = </span><span class="c4 c25">input</span><span class="c4 c28">(</span><span class="c40 c4">&quot;enter a {}&quot;</span><span class="c4 c28">.format(word))</span></p><p class="c1"><span class="c4 c28">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;new_words.append(new_word)</span></p><p class="c1"><span class="c4 c28">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c7 c4 c5">for </span><span class="c4 c28">index</span><span class="c7 c4">, </span><span class="c4 c28">hint </span><span class="c7 c4 c5">in </span><span class="c4 c25">enumerate</span><span class="c4 c28">(hints):</span></p><p class="c1"><span class="c4 c28">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;mls = mls.replace(hint</span><span class="c7 c4">, </span><span class="c4 c28">new_words[index]</span><span class="c7 c4">, </span><span class="c4 c20">1</span><span class="c4 c28">)</span></p><p class="c1"><span class="c4 c28">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c7 c4 c5">print</span><span class="c4 c28">(</span><span class="c40 c4">&#39;</span><span class="c7 c4">\n</span><span class="c40 c4">&#39;</span><span class="c4 c28">)</span></p><p class="c1"><span class="c4 c28">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c7 c4 c5">print</span><span class="c4 c28">(mls)</span></p><p class="c1"><span class="c4 c28">&nbsp; &nbsp;</span><span class="c7 c4 c5">else</span><span class="c4 c28">:</span></p><p class="c1"><span class="c4 c28">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c7 c4 c5">print</span><span class="c4 c28">(</span><span class="c40 c4">&quot;invalid mls&quot;</span><span class="c4 c28">)</span></p><p class="c1 c9"><span class="c4 c28"></span></p><p class="c1"><span class="c4 c28">mad_libs(text)</span></p><p class="c1 c9"><span></span></p><h2 class="c1 c13" id="h.2v9hx1dfl3u0"><span>Bringing It All Together</span></h2><p class="c1"><span class="c3">&ldquo;The magic of myth and legend has come true in our time. One types the correct incantation on a keyboard, and a display screen comes to life, showing things that never were nor could be&rdquo;....</span></p><p class="c1"><span class="c3">&mdash; Frederick Brooks</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;In this chapter, we will see how powerful programming can be by building a web scraper, a program that extracts data from a website. Learning to scrape data from websites is incredibly powerful. It gives you the ability to extract any data you want from the largest collection of information that has ever existed. Seeing the power of web scrapers, and how easy they are to build, is one of the reasons I got hooked up on programming, and I hope it has the same effect on you. </span></p><h3 class="c1 c13" id="h.ritur7xu47m3"><span>HTML</span></h3><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Before we build our web scraper, we need a quick primer on HTML, or hypertext markup language. HTML is one of the fundamental technologies used to build websites, along with CSS and JavaScript. You can think of HTML as its own little language, used to give a website structure. HTML is made up of tags that a web browser uses to layout a web page. In fact, you can build an entire website using only HTML. It won&rsquo;t be interactive or look very good, because JavaScript is what makes websites interactive, and CSS is what gives them style, but it will be a website. Here is an example of a website that will display the text &ldquo;Hello World!&rdquo;:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c4 c61">&lt;!--This is a comment in HTML. Save this file as index.html--&gt;</span></p><p class="c1 c9"><span class="c4 c28"></span></p><p class="c1 c10"><span class="c4 c62">&lt;html </span><span class="c93 c4">lang=</span><span class="c40 c4">&quot;en&quot;</span><span class="c4 c62">&gt;</span></p><p class="c1 c10"><span class="c4 c62">&lt;head&gt;</span></p><p class="c1 c10"><span class="c4 c62">&nbsp; &lt;meta </span><span class="c93 c4">charset=</span><span class="c40 c4">&quot;UTF-8&quot;</span><span class="c4 c62">&gt;</span></p><p class="c1 c10"><span class="c4 c62">&nbsp; &lt;title&gt;</span><span class="c4 c28">My Website</span><span class="c4 c62">&lt;/title&gt;</span></p><p class="c1 c10"><span class="c4 c62">&lt;/head&gt;</span></p><p class="c1 c10"><span class="c4 c62">&lt;body&gt;</span></p><p class="c1 c10"><span class="c4 c62">&nbsp; &nbsp;</span><span class="c4 c28">Hello, World!</span></p><p class="c1 c10"><span class="c4 c28">&nbsp; </span><span class="c4 c62">&lt;a </span><span class="c4 c93">href=</span><span class="c40 c4">&quot;https://news.ycombinator.com/&quot;</span><span class="c4 c62">&gt;</span><span class="c4 c28">here</span><span class="c4 c62">&lt;/a&gt;</span></p><p class="c1 c10"><span class="c4 c62">&lt;/body&gt;</span></p><p class="c1 c10"><span class="c4 c62">&lt;/html&gt;</span></p><p class="c1 c9"><span class="c4"></span></p><p class="c1"><span class="c3">Take a minute to study this code. Now save this HTML into a file and open with your web browser</span><span class="c3">&nbsp;by clicking on it</span><span class="c3">, you will be able to see a website that says &ldquo;Hello World!&rdquo; and a link to a the Y Combinator website. Your web browser uses the different tags in our HTML to figure out how to display this website. </span></p><p class="c0"><span class="c3">Tags have a beginning tag and closing tag, often with something like text in between. For example, your browser displays the text in between the </span><span class="c4">&lt;title&gt; &lt;/title&gt;</span><span class="c3">&nbsp;tags in the tab of your browser. Anything in between the </span><span class="c4">&lt;body&gt; &lt;/body&gt;</span><span class="c3">&nbsp;tags, makes up the actual website. Anything inside </span><span class="c4">&lt;a&gt; &lt;/a&gt;</span><span class="c3">&nbsp;tags is a link. There is a lot more to HTML, but this is all you need to know in order to build your first web scraper. </span></p><h3 class="c1 c13" id="h.sn1ssbd6koei"><span>Scrape Google News</span></h3><p class="c0"><span class="c3">Now we can build a scraper that fetches all of the headlines from Google News. We will use the BeautifulSoup library for HTML parsing (converting HTML to Python), so install it with:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c0"><span class="c3">pip install beautifulsoup4==4.4.1</span><sup><a href="#cmnt44" id="cmnt_ref44">[ar]</a></sup></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">Once BeautifulSoup is installed, we can get Google News&rsquo;s HTML using Python&rsquo;s built-in </span><span class="c4">urllib2</span><span class="c3">&nbsp;library. Start by importing both modules:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c10"><span class="c7 c4 c5">import </span><span class="c4 c28">urllib2</span></p><p class="c1 c10"><span class="c7 c4 c5">from </span><span class="c4 c28">bs4 </span><span class="c7 c4 c5">import </span><span class="c4 c28">BeautifulSoup</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">Next we create a scraper class</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c0"><span class="c7 c4 c5">class </span><span class="c4 c28 c5">Scraper</span><span class="c4 c28">:</span></p><p class="c1 c10"><span class="c4 c28">&nbsp; &nbsp;</span><span class="c7 c4 c5">def </span><span class="c29 c4">__init__</span><span class="c4 c28">(</span><span class="c4 c8">self</span><span class="c7 c4">, </span><span class="c4 c28">site):</span></p><p class="c1 c10"><span class="c4 c28">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c4 c8">self</span><span class="c4 c28">.site = site</span></p><p class="c1 c9 c10"><span class="c4 c28"></span></p><p class="c1 c10"><span class="c4 c28">&nbsp; &nbsp;</span><span class="c7 c4 c5">def </span><span class="c4 c28 c5">scrape</span><span class="c4 c28">(</span><span class="c4 c8">self</span><span class="c4 c28">):</span></p><p class="c1 c10"><span class="c4 c28">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c7 c4 c5">pass</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">Our method takes a website to scrape from, and has a method called scrape which we are going to call whenever we want to scrape the website we passed in. </span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">Now we can start defining our scrape method. &nbsp;</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c0"><span class="c7 c4 c5">def </span><span class="c4 c28 c5">scrape</span><span class="c4 c28">(</span><span class="c4 c8">self</span><span class="c4 c28">):</span></p><p class="c1 c10"><span class="c4 c28">&nbsp; &nbsp;response = urllib2.urlopen(</span><span class="c4 c8">self</span><span class="c4 c28">.site)</span></p><p class="c1 c10"><span class="c4 c28">&nbsp; &nbsp;</span><span class="c4 c61">html </span><span class="c4 c28">= response.read()</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">The </span><span class="c4">urlopen()</span><span class="c3">&nbsp;function makes a request to Google News and returns the response,which includes Google News&rsquo;s HTML. We save the response in our </span><span class="c4">response</span><span class="c3">&nbsp;variable and pass it into </span><span class="c4">response.read()</span><span class="c3">&nbsp;which now holds all of the HTML from Google News. &nbsp;This is all we need in order to extract all the links from Google News. However, we still need to parse the HTML. Parsing HTML means reading it into our program and giving it structure with our code, such as turning each HTML tag into a Python object, which we will do using the Beautiful Soup library. First, we create a </span><span class="c4">BeautifulSoup</span><span class="c3">&nbsp;object and pass in our html and the string &lsquo; html.parser&rsquo; to let Beautiful Soup know we are parsing HTML:</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p><p class="c1 c10"><span class="c7 c4 c5">def </span><span class="c4 c28 c5">scrape</span><span class="c4 c28">(</span><span class="c4 c8">self</span><span class="c4 c28">):</span></p><p class="c1 c10"><span class="c4 c28">&nbsp; &nbsp;response = urllib2.urlopen(</span><span class="c4 c8">self</span><span class="c4 c28">.site)</span></p><p class="c1 c10"><span class="c4 c28">&nbsp; &nbsp;html = response.read()</span></p><p class="c1 c10"><span class="c4 c28">&nbsp; </span><span class="c4 c28">&nbsp;soup</span><span class="c4 c61">&nbsp;</span><span class="c4 c28">= BeautifulSoup(html</span><span class="c7 c4">, </span><span class="c40 c4">&#39;html.parser&#39;</span><span class="c4 c28">)</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">Now we can easily extract the links from our HTML, but first we will get the &lt;title&gt; tag of the HTML we passed in to demonstrate how Beautiful Soup works:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c0"><span class="c7 c4 c5">import </span><span class="c4 c28">urllib2</span></p><p class="c0"><span class="c7 c4 c5">from </span><span class="c4 c28">bs4 </span><span class="c7 c4 c5">import </span><span class="c4 c28">BeautifulSoup</span></p><p class="c0 c9"><span class="c4 c28"></span></p><p class="c0 c9"><span class="c4 c28"></span></p><p class="c0"><span class="c7 c4 c5">class </span><span class="c4 c28 c5">Scraper</span><span class="c4 c28">:</span></p><p class="c0"><span class="c4 c28">&nbsp; &nbsp;</span><span class="c7 c4 c5">def </span><span class="c29 c4">__init__</span><span class="c4 c28">(</span><span class="c4 c8">self</span><span class="c7 c4">, </span><span class="c4 c28">site):</span></p><p class="c0"><span class="c4 c28">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c4 c8">self</span><span class="c4 c28">.site = site</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c10"><span class="c7 c4 c5">def </span><span class="c4 c28 c5">scrape</span><span class="c4 c28">(</span><span class="c4 c8">self</span><span class="c4 c28">):</span></p><p class="c1 c10"><span class="c4 c28">&nbsp; &nbsp;response = urllib2.urlopen(</span><span class="c4 c8">self</span><span class="c4 c28">.site)</span></p><p class="c1 c10"><span class="c4 c28">&nbsp; &nbsp;html = response.read()</span></p><p class="c1 c10"><span class="c4 c28">&nbsp; &nbsp;soup</span><span class="c4 c61">&nbsp;</span><span class="c4 c28">= BeautifulSoup(html</span><span class="c7 c4">, </span><span class="c40 c4">&#39;html.parser&#39;</span><span class="c4 c28">)</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;# This is just an example and will not be in our final scraper.</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp;</span><span class="c2">&nbsp; </span><span class="c4 c28">print(soup.title)</span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p><p class="c0"><span class="c4 c28">Scraper</span><span class="c4 c28">().scrape(</span><span class="c40 c4">&#39;https://news.google.com/&#39;</span><span class="c4 c28">)</span></p><p class="c1 c9"><span class="c4"></span></p><p class="c1"><span class="c4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; &lt;title&gt;Google News&lt;/title&gt;</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">BeautifulSoup has different methods like title you can call on our soup object to return subsets of the HTML in different ways. For example, now can print out the links from Google News with:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c10"><span class="c7 c4 c5">def </span><span class="c4 c28 c5">scrape</span><span class="c4 c28">(</span><span class="c4 c8">self</span><span class="c4 c28">):</span></p><p class="c1 c10"><span class="c4 c28">&nbsp; &nbsp;response = urllib2.urlopen(</span><span class="c4 c8">self</span><span class="c4 c28">.site)</span></p><p class="c1 c10"><span class="c4 c28">&nbsp; &nbsp;html = response.read()</span></p><p class="c1 c10"><span class="c4 c28">&nbsp; &nbsp;soup = BeautifulSoup(html</span><span class="c7 c4">, </span><span class="c40 c4">&#39;html.parser&#39;</span><span class="c4 c28">)</span></p><p class="c1 c10"><span class="c4 c28">&nbsp; &nbsp;</span><span class="c7 c4 c5">for </span><span class="c4 c28">tag </span><span class="c7 c4 c5">in </span><span class="c4 c28">soup.find_all(</span><span class="c40 c4">&#39;a&#39;</span><span class="c4 c28">):</span></p><p class="c1 c10"><span class="c4 c28">&nbsp; &nbsp; &nbsp; &nbsp;link_title = tag.get(</span><span class="c40 c4">&#39;href&#39;</span><span class="c4 c28">)</span></p><p class="c1 c10"><span class="c4 c28">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c7 c4 c5">if </span><span class="c4 c28">link_title </span><span class="c7 c4 c5">and </span><span class="c40 c4">&#39;html&#39; </span><span class="c7 c4 c5">in </span><span class="c4 c28">link_title:</span></p><p class="c1 c10"><span class="c4 c28">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c7 c4 c5">print</span><span class="c4 c28">(</span><span class="c40 c4">&quot;</span><span class="c7 c4">\n</span><span class="c40 c4">&quot; </span><span class="c4 c28">+ tag.get(</span><span class="c40 c4">&#39;href&#39;</span><span class="c4 c28">))</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c4">find_all()</span><span class="c3">&nbsp;is another method we can call on soup objects. It takes a string representing an HTML tag as a parameter ,and returns a </span><span class="c4">ResultSet</span><span class="c3">&nbsp;object, which is similar to a list in that you can iterate through it. The </span><span class="c4">ResultSet</span><span class="c3">&nbsp;object contains </span><span class="c4">Tag</span><span class="c3">&nbsp;objects representing each HTML tag found. We iterate through the </span><span class="c4">ResultSet</span><span class="c3">, storing each tag object in the variable </span><span class="c4">tag</span><span class="c3">. We don&rsquo;t want the entire </span><span class="c4">tag</span><span class="c3">&nbsp;object, we only want the title of the HTML tag, which we get by using the method </span><span class="c4">get()</span><span class="c3">, passing in the string &lsquo;href&rsquo;, and saving it in the variable </span><span class="c4">link_title</span><span class="c3">. </span></p><p class="c0"><span class="c3">The last thing we do is to check the </span><span class="c4">link_title</span><span class="c3">&nbsp;to make sure it does not equal </span><span class="c4">None</span><span class="c3">&nbsp;with </span><span class="c4">if link_title</span><span class="c3">, because the link_title could be empty, which would crash our program. We also make sure &lsquo;html&rsquo; is in the link_title, because we don&rsquo;t want to print Google&rsquo;s internal links. If the </span><span class="c4">link_title</span><span class="c3">&nbsp;passes both of these tests, we use &lsquo;\n&rsquo; to print a newline and then print &nbsp;it. Here is our full program:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c10"><span class="c7 c4 c5">import </span><span class="c4 c28">urllib2</span></p><p class="c1 c10"><span class="c7 c4 c5">from </span><span class="c4 c28">bs4 </span><span class="c7 c4 c5">import </span><span class="c4 c28">BeautifulSoup</span></p><p class="c1 c9 c10"><span class="c4 c28"></span></p><p class="c1 c9 c10"><span class="c4 c28"></span></p><p class="c1 c10"><span class="c7 c4 c5">class </span><span class="c4 c28 c5">Scraper</span><span class="c4 c28">:</span></p><p class="c1 c10"><span class="c4 c28">&nbsp; &nbsp;</span><span class="c7 c4 c5">def </span><span class="c29 c4">__init__</span><span class="c4 c28">(</span><span class="c4 c8">self</span><span class="c7 c4">, </span><span class="c4 c28">site):</span></p><p class="c1 c10"><span class="c4 c28">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c4 c8">self</span><span class="c4 c28">.site = site</span></p><p class="c1 c9 c10"><span class="c4 c28"></span></p><p class="c1 c10"><span class="c4 c28">&nbsp; &nbsp;</span><span class="c7 c4 c5">def </span><span class="c4 c28 c5">scrape</span><span class="c4 c28">(</span><span class="c4 c8">self</span><span class="c4 c28">):</span></p><p class="c1 c10"><span class="c4 c28">&nbsp; &nbsp; &nbsp; &nbsp;response = urllib2.urlopen(</span><span class="c4 c8">self</span><span class="c4 c28">.site)</span></p><p class="c1 c10"><span class="c4 c28">&nbsp; &nbsp; &nbsp; &nbsp;html = response.read()</span></p><p class="c1 c10"><span class="c4 c28">&nbsp; &nbsp; &nbsp; &nbsp;soup = BeautifulSoup(html</span><span class="c7 c4">, </span><span class="c40 c4">&#39;html.parser&#39;</span><span class="c4 c28">)</span></p><p class="c1 c10"><span class="c4 c28">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c7 c4 c5">for </span><span class="c4 c28">tag </span><span class="c7 c4 c5">in </span><span class="c4 c28">soup.find_all(</span><span class="c40 c4">&#39;a&#39;</span><span class="c4 c28">):</span></p><p class="c1 c10"><span class="c4 c28">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;link_title = tag.get(</span><span class="c40 c4">&#39;href&#39;</span><span class="c4 c28">)</span></p><p class="c1 c10"><span class="c4 c28">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c7 c4 c5">if </span><span class="c4 c28">link_title </span><span class="c7 c4 c5">and </span><span class="c4 c40">&#39;html&#39; </span><span class="c7 c4 c5">in </span><span class="c4 c28">link_title:</span></p><p class="c1 c10"><span class="c4 c28">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c7 c4 c5">print</span><span class="c4 c28">(</span><span class="c40 c4">&quot;</span><span class="c7 c4">\n</span><span class="c40 c4">&quot; </span><span class="c4 c28">+ tag.get(</span><span class="c40 c4">&#39;href&#39;</span><span class="c4 c28">))</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c4 c28">Scraper</span><span class="c4 c28">().scrape(</span><span class="c40 c4">&#39;https://news.google.com/&#39;</span><span class="c4 c28">)</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">Run the scraper, and you should see a result similar to this:</span></p><p class="c1"><span class="c17 c3"><a class="c18" href="https://www.google.com/url?q=https://www.washingtonpost.com/world/national-security/in-foreign-bribery-cases-leniency-offered-to-companies-that-turn-over-employees/2016/04/05/d7a24d94-fb43-11e5-9140-e61d062438bb_story.html&amp;sa=D&amp;ust=1467337427124000&amp;usg=AFQjCNF53bg5b-7zXwX914EnTULJtmgTVw">https://www.washingtonpost.com/world/national-security/in-foreign-bribery-cases-leniency-offered-to-companies-that-turn-over-employees/2016/04/05/d7a24d94-fb43-11e5-9140-e61d062438bb_story.html</a></span></p><p class="c1 c9"><span class="c17 c3"><a class="c18" href="https://www.google.com/url?q=https://www.washingtonpost.com/world/national-security/in-foreign-bribery-cases-leniency-offered-to-companies-that-turn-over-employees/2016/04/05/d7a24d94-fb43-11e5-9140-e61d062438bb_story.html&amp;sa=D&amp;ust=1467337427125000&amp;usg=AFQjCNG_HOfW-An8HyDMSkbMDul-g9KBsg"></a></span></p><p class="c1"><span class="c17 c3"><a class="c18" href="https://www.google.com/url?q=http://www.appeal-democrat.com/news/unit-apartment-complex-proposed-in-marysville/article_bd6ea9f2-fac3-11e5-bfaf-4fbe11089e5a.html&amp;sa=D&amp;ust=1467337427126000&amp;usg=AFQjCNGdyPEMCzdW486YOQCHtkatpkKEkw">http://www.appeal-democrat.com/news/unit-apartment-complex-proposed-in-marysville/article_bd6ea9f2-fac3-11e5-bfaf-4fbe11089e5a.html</a></span></p><p class="c1 c9"><span class="c17 c3"><a class="c18" href="https://www.google.com/url?q=http://www.appeal-democrat.com/news/unit-apartment-complex-proposed-in-marysville/article_bd6ea9f2-fac3-11e5-bfaf-4fbe11089e5a.html&amp;sa=D&amp;ust=1467337427126000&amp;usg=AFQjCNGdyPEMCzdW486YOQCHtkatpkKEkw"></a></span></p><p class="c1"><span class="c17 c3"><a class="c18" href="https://www.google.com/url?q=http://www.appeal-democrat.com/news/injuries-from-yuba-city-bar-violence-hospitalize-groom-to-be/article_03e46648-f54b-11e5-96b3-5bf32bfbf2b5.html&amp;sa=D&amp;ust=1467337427127000&amp;usg=AFQjCNF-GVNt_PT6hv-f3SQO2UYMMmis3w">http://www.appeal-democrat.com/news/injuries-from-yuba-city-bar-violence-hospitalize-groom-to-be/article_03e46648-f54b-11e5-96b3-5bf32bfbf2b5.html</a></span></p><p class="c1 c9"><span class="c17 c3"><a class="c18" href="https://www.google.com/url?q=http://www.appeal-democrat.com/news/injuries-from-yuba-city-bar-violence-hospitalize-groom-to-be/article_03e46648-f54b-11e5-96b3-5bf32bfbf2b5.html&amp;sa=D&amp;ust=1467337427127000&amp;usg=AFQjCNF-GVNt_PT6hv-f3SQO2UYMMmis3w"></a></span></p><p class="c1"><span class="c3">...</span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.xpra7190ndeo"><span>Wrapping Up</span></h3><p class="c0"><span class="c3">Now that you have all of Google News&rsquo;s headlines available in your program, the possibilities are limitless. You could write a program to analyze the most used words in the headlines, and build a word cloud to visualize it. You could build a program to analyze the sentiment of the headlines, and see if it has any correlation with the stock market. As you get better at web scraping, all of the information in the world will be open to you, and I hope that excites you as much as it excites me. </span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.dogv21if61ja"><span>Challenge</span></h3><p class="c1"><span class="c3">Modify the Google Scraper to save the headlines in a CSV file. </span></p><h2 class="c1 c13" id="h.xp2gmr7330nw"><span>Practice</span><sup><a href="#cmnt45" id="cmnt_ref45">[as]</a></sup></h2><p class="c1 c9"><span></span></p><h3 class="c1 c13" id="h.vo1ohyuftukp"><span>Exercises </span></h3><ol class="c45 lst-kix_diq9v8p1rag-0 start" start="1"><li class="c1 c10 c38"><span>Build a scraper for another website.</span></li><li class="c1 c10 c38"><span>Write five regular expressions in Python.</span></li><li class="c1 c9 c10 c38"><span></span></li></ol><p class="c1 c9"><span></span></p><h3 class="c1 c13" id="h.p62yogim0gti"><span>Read</span></h3><p class="c1 c9"><span></span></p><h1 class="c1 c13" id="h.iboyy11j25wv"><span class="c3 c71">Part IV </span><span class="c71">Study</span><span class="c3 c71">&nbsp;Computer Science</span></h1><h2 class="c1 c13" id="h.yhwshf5cz9zs"><span>Computer Architecture</span></h2><p class="c1"><span class="c3 c5">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">In this chapter, we cover the basics of computer architecture. In the last chapter of this book, Further Learning, I suggest you check out the the popular programming website Stack Overflow. You should, however also understand what the name Stack Overflow means. In order to understand do that, you need to understand a little about how memory works. </span></p><p class="c1"><span class="c3 c5">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">Programs have two areas called the stack and the heap that manage memory. </span></p><h3 class="c1 c13" id="h.yxytc36erslc"><span>CPU</span></h3><p class="c1"><span class="c3">At a high level a computer is made up of a CPU and memory. The CPU is the part of a computer that executes the instructions provided by a program. A computer can have one or more CPUS: a &nbsp;computer with multiple CPUs is said to have a multi-core processor. &nbsp;</span></p><p class="c1"><span class="c3">A CPU has a clock generator that produces &ldquo;clock cycles&rdquo;. In a CISC, or Complex Instruction Set processor, each instruction takes one or more &ldquo;clock cycles&rdquo;, whereas RISC, or Reduced Instruction Set Computing processors, are faster, able to execute multiple instructions per clock cycle. Clock speed refers to the speed a microprocessor executes instructions.</span></p><h3 class="c1 c13" id="h.hhkw9dxis0fa"><span>Memory</span></h3><p class="c1"><span class="c3 c5">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">Computer&rsquo;s have two types of memory, RAM and ROM. RAM stands for random access memory and is volatile, meaning it is erased when a computer turns off. All arithmetic and logic operations take place in ram. ROM stands for read only memory. It is not volatile, it persists after a computer is turned off and is used for the most fundamental parts of an operating system needed when a computer starts up.</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Everything in memory is stored in binary, sequences of zeros and ones. The zeros and ones are stored in chips made up of millions or billions of transistors. &nbsp;Each transistor can be turned on or off using a flow of electricity. When a transistor is turned off it represents a zero in binary, and when a transistor is turned on it represents a one.</span></p><p class="c1"><span class="c3">&nbsp;Memory is not where everything on a computer is stored, despite the name sounding like it is. Memory is used in combination with the CPU to execute tasks, like arithmetic operations. When you store information in a database for example, it is written to disk storage, not memory. &nbsp;</span></p><h3 class="c1 c13" id="h.hth65tvubinq"><span>Devices</span></h3><h4 class="c1 c13" id="h.b73yaagxx6vh"><span>I/O</span></h4><p class="c1"><span class="c3">The CPU combined with memory is the brain of a computer. I/O is the transfer of data to or from the brain by other devices. Reading and writing data from disk storage is an example of I/O. </span></p><h3 class="c1 c13" id="h.jogl9q6rf2ja"><span>Network</span><sup><a href="#cmnt46" id="cmnt_ref46">[at]</a></sup></h3><h3 class="c1 c13" id="h.f0w897framwh"><span>Wrapping Up</span></h3><p class="c1"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`23</span></p><p class="c1"><span>]\o=[ &nbsp;</span></p><p class="c1"><span>swertyu0i-</span></p><h3 class="c1 c13" id="h.4lss9raky8vb"><span>Challenge</span><sup><a href="#cmnt47" id="cmnt_ref47">[au]</a></sup></h3><h2 class="c1 c13" id="h.rx2tv5c4th4w"><span>Low Level </span></h2><h3 class="c1 c13" id="h.kj35lqq1pd9s"><span>Binary </span></h3><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Computers can only understand binary, so in order to understand how a computer works, you should have a basic understanding of binary. When you normally count, you count in base ten, which means we represent every number in the world using only ten digits. The &ldquo;base&rdquo; of a counting system is the number of numbers used to represent all the number in the world. In the base 10 counting system, once we get over the number nine, we recombine numbers from one to ten to create new numbers:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0 </span><span class="c88 c3">1</span><span class="c3">&nbsp;2 3 4 5 6 7 8 9 </span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3 c88">11</span><span class="c3">&nbsp;</span><span class="c88 c3">1</span><span class="c3">2 </span><span class="c88 c3">1</span><span class="c3">3 14 15 16 17 18 19 20</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Base two, &nbsp;is a system of counting just like base ten. However, instead of combining existing numbers after ten numbers, base two starts doing it after two numbers</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0 -&gt; &nbsp; &nbsp;0</span></p><p class="c0"><span class="c3">1 -&gt; &nbsp; &nbsp;1</span></p><p class="c0"><span class="c3">10 -&gt; &nbsp;2</span></p><p class="c0"><span class="c3">11 -&gt; &nbsp;3</span></p><p class="c0 c9"><span class="c3"></span></p><p class="c1"><span class="c3">0 and 1 are the same as base ten. However, once we get to 2, we&rsquo;ve gone past two numbers, and we need to combine our first two numbers to create a new number. Hence &ldquo;10&rdquo; is combined to represent 2. Each number starting from the left represents whether there is a power of 2. So for example, &ldquo;10&rdquo; means there are zero 2 ** 0&rsquo;s and 1 2 **1:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c0"><span class="c3">10</span></p><p class="c1 c10"><span class="c3">2 + 0 = 2 &nbsp; </span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.fcmct7vaivuo"><span>High-level vs. Low Level Languages</span></h3><p class="c1"><span class="c3">A language becomes higher-level the further away it gets from the direct instructions executed by the CPU. Programming languages vary in how low-level they are. People teaching themselves to program usually start by learning a high-level language like Python, JavaScript or Ruby. Computers only speak binary. Machine code is binary that can be executed by the CPU. Everything above machine code is an abstraction. Above machine code you have assembly code. Assembly code is described on Stack Overflow as &ldquo;plain-text and (somewhat) human read-able source code that has a mostly direct 1:1 analog with machine instructions.&rdquo; 1 Above assembly code you have the C programming language. </span></p><h3 class="c1 c13" id="h.fde5b89kb9d7"><span>Dynamic vs. Statically Typed Languages</span></h3><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Programming languages can be either dynamically or statically typed. Python and Ruby are examples of dynamically typed languages. In a dynamically typed language, you can declare a variable as one type, and later change that same variable to a value of a different type. For example, this is allowed in Python:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c10"><span class="c3">x = 5</span></p><p class="c1 c10"><span class="c3">x = &ldquo;Hello World&rdquo;</span></p><p class="c1 c9 c10"><span class="c3"></span></p><p class="c1"><span class="c3">In a statically typed language, trying to change a variable from one type to another will cause an error because in a statically typed language, once you declare a variable&rsquo;s type, you cannot change it. Understanding the difference between statically and dynamically typed languages will payoff with hours of fun arguing with your friends with Computer Science degrees about whether or not the former is better than the latter.</span></p><h3 class="c1 c13" id="h.5guxwmfw6uw"><span>Math</span><sup><a href="#cmnt48" id="cmnt_ref48">[av]</a></sup></h3><h3 class="c1 c13" id="h.clgqz0r5o9h4"><span>Character Representation</span></h3><p class="c1"><span class="c3 c5">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">As we learned earlier, computers only understand binary. So how do computers work with characters? The answer is character encoding schemes like ASCII and Unicode. To a computer, characters do not exist. Characters are represented using encoding schemes like ASCII to map characters to binary numbers.</span></p><p class="c1"><span class="c3">&nbsp;</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">This chart shows how each character is mapped to a number. For example, 97 is mapped to the letter &ldquo;a&rdquo;. A program using ASCII would store an &ldquo;a&rdquo; as 1100001, or 97 in binary. When the same program needs to retrieve the letter &ldquo;a&rdquo;, it looks up 97 in the ASCII table and sees it represents the letter &ldquo;a&rdquo;. </span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;You can use Python to easily get the number a character maps to in ASCII. The function &ldquo;ord&rdquo; takes a character and returns the number it maps to:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c10"><span class="c3">ord(&ldquo;a&rdquo;)</span></p><p class="c1 c10"><span class="c3">&gt;&gt; 97</span></p><p class="c1 c9 c10"><span class="c3"></span></p><p class="c1 c10"><span class="c3">ord(&ldquo;z&rdquo;)</span></p><p class="c1 c10"><span class="c3">&gt;&gt; 122 </span></p><p class="c1 c9 c10"><span class="c3"></span></p><h3 class="c1 c13" id="h.48cp62o4ibvv"><span>Wrapping Up</span></h3><p class="c1 c9"><span></span></p><h3 class="c1 c13" id="h.r1b9ddl7csd4"><span>Challenge</span><sup><a href="#cmnt49" id="cmnt_ref49">[aw]</a></sup></h3><h2 class="c1 c13" id="h.otwq7pynty22"><span>Data Structures &amp; Algorithms</span></h2><p class="c1"><span class="c3">&ldquo;I will, in fact, claim that the difference between a bad programmer and a good one is whether he considers his code or his data structures more important. Bad programmers worry about the code. Good programmers worry about data structures and their relationships.&rdquo;</span></p><p class="c1"><span class="c3">&mdash; Linus Torvalds</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">This chapter is a light introduction to algorithms and data structures, perhaps the important subject in Computer Science. The title of the influential book </span><span class="c15 c3">Algorithms + Data Structures = Programs </span><span class="c3">summarizes their importance. My goal in this chapter is to introduce you to the subject, and clarify some things I found confusing when I was learning about them (which I still am). In addition to reading this chapter, you definitely need to read more about algorithms and data structures outside of this book, and also spend a lot of time practicing the concepts introduced here. Many of the examples in this chapter come from the amazing book </span><span class="c15 c3">Python Algorithms and Data Structures</span><span class="c3">&nbsp;</span><span class="c11 c3">by </span><span class="c11 c3 c33 c22">Brad Miller and David Ranum. </span><span class="c11 c3">It is one of my favorite books, available online for free at: </span><span class="c3">http://interactivepython.org.</span></p><h3 class="c1 c13" id="h.ospe9hf18tpz"><span>What Are Algorithms &amp; Data Structures?</span></h3><p class="c1"><span class="c3">An algorithm is a series of steps that can be followed to solve a problem. The problem could be anything, like sorting or searching a list, or traverse a tree. </span></p><p class="c1"><span class="c3 c5">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">A data structure is a way to store and organize information. Data structures are fundamental to programming, and whatever programming language you use will come with built in data structures. &nbsp;Common data structures include hash tables, stacks and lists. Data structures come with different tradeoffs, with certain data structures being better suited for specific tasks than others. </span></p><h3 class="c1 c13" id="h.y9pg0zkthft1"><span>Big O Notation</span><sup><a href="#cmnt50" id="cmnt_ref50">[ax]</a></sup></h3><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;In Computer Science, we solve problems using algorithms. But what if you come up with two different algorithms to solve the same problem? How do you decide which is best? Big O Notation gives you a framework for deciding if one algorithm is better than another by looking at the number of steps each algorithm takes, and choosing the one that takes the least amount of steps. </span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;We can use an equation like T(n) = n to describe an algorithm </span></p><h3 class="c1 c13" id="h.fq1ereenkrkw"><span>Modulo</span></h3><p class="c1"><span class="c3">The modulo operator &ldquo;%&rdquo; returns the remainder of two numbers when you divide them. For example &ldquo;2 % 2&rdquo; returns 0, whereas &ldquo;3 % 2&rdquo; returns 1. Modulo is helpful in solving a variety of problems like Fizzbuzz, a popular first interview question designed to weed out people that cannot program. The question was introduced by Imran Ghory in his blog post </span><span class="c15 c3">Using FizzBuzz to Find Developers who Grok Coding</span><span class="c3">. If you know the correct approach to the problem, it is easy to solve, whereas if you don&rsquo;t, it can appear complicated. The problem is usually given as:</span></p><p class="c1"><span class="c3">Write a program that prints the numbers from 1 to 100. But for multiples of three print &ldquo;Fizz&rdquo; instead of the number and for the multiples of five print &ldquo;Buzz&rdquo;. For numbers which are multiples of both three and five print &ldquo;FizzBuzz&rdquo;.</span></p><p class="c1"><span class="c3">The key to solving this problem is using modulo. To solve this problem you simply need to iterate from 1 to 100 and check if each number is divisible by 3, 5 or both. Here is the solution:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c10"><span class="c3 c5">def fizz_buzz</span><span class="c3">():</span></p><p class="c1 c10"><span class="c3">&nbsp; &nbsp;</span><span class="c3 c5">for </span><span class="c3">i </span><span class="c3 c5">in </span><span class="c3">range(0, 101):</span></p><p class="c1 c10"><span class="c3">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c3 c5">if </span><span class="c3">i % 3 == 0 </span><span class="c3 c5">and </span><span class="c3">i % 5 == 0:</span></p><p class="c1 c10"><span class="c3">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c3 c5">print </span><span class="c3">&#39;FizzBuzz&#39;</span></p><p class="c1 c10"><span class="c3">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c3 c5">elif </span><span class="c3">i % 3 == 0:</span></p><p class="c1 c10"><span class="c3">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c3 c5">print </span><span class="c3">&#39;Fizz&#39;</span></p><p class="c1 c10"><span class="c3">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c3 c5">elif </span><span class="c3">i % 5 == 0:</span></p><p class="c1 c10"><span class="c3">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c3 c5">print </span><span class="c3">&#39;Buzz&#39;</span></p><p class="c1 c10"><span class="c3">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c3 c5">else</span><span class="c3">:</span></p><p class="c1 c10"><span class="c3">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c3 c5">print </span><span class="c3">i</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">We start by iterating through the numbers 1 to 100 with a for loop. Then we simply check each of the conditions. We need to check if the number is divisible by 3 or 5 first, because if it is, we can move to the next number. This is not true with being divisible by either 5 or 3, because if either are true, we still have to check if the number is divisible by both. We then can check if the number is divisible by 3 or 5 (in any order). Finally, if none of the conditions are true, we simply print the number.</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Here is another problem where using modulo is the key to figuring out the answer:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Rotate an array of </span><span class="c15 c3">n</span><span class="c3">&nbsp;elements to the right by </span><span class="c15 c3">k</span><span class="c3">&nbsp;steps.</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">For example, with </span><span class="c15 c3">n</span><span class="c3">&nbsp;= 7 and </span><span class="c15 c3">k</span><span class="c3">&nbsp;= 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4] ie. you are moving every element in the list k positions.</span></p><p class="c1"><span class="c3">When you solve this problem, your first instinct might be to simply add n to the index of each number and move the number to the new position. The problem is this does not work. &nbsp;The key to solving this problem is once again modulo. Say we had a list of numbers from &nbsp;1-12. We can think of this list as a clock. If we start at 12 o&#39;clock, and go around the clock twelve hours, we are back to where we started. </span></p><p class="c1"><span class="c3">We can achieve this by taking the current index, adding the new index and getting the remainder. If you take our list of 12 &nbsp;numbers and you want to calculate where the number 12 would be (starting at index 11) if we added 12 to the index, you can calculate that with 11 + </span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.rapqos3xtqu5"><span>Bubble Sort</span></h3><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;A sorting algorithm is an algorithm that takes a group of numbers and puts them in a certain order. There are many different algorithms such as selection sort, insertion sort, shell sort merge sort and quicksort. In this section we will implement bubble sort, a sorting algorithm that is not very efficient&mdash;but easy to understand and useful for understanding sorting algorithms. Here is an implementation of bubble sort:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c11 c7 c3 c5">def </span><span class="c2 c11 c5">bubble_sort</span><span class="c2 c11">(num_list):</span><sup><a href="#cmnt51" id="cmnt_ref51">[ay]</a></sup></p><p class="c1 c10"><span class="c2 c11">&nbsp; &nbsp;</span><span class="c11 c15 c3 c30">&quot;&quot;&quot;</span></p><p class="c1 c10"><span class="c11 c15 c3 c30">&nbsp; &nbsp;</span><span class="c11 c15 c3 c30 c5">:param</span><span class="c11 c15 c3 c30">&nbsp;num_list: List of numbers</span></p><p class="c1 c10"><span class="c11 c15 c3 c30">&nbsp; &nbsp;</span><span class="c11 c15 c3 c30 c5">:return</span><span class="c11 c15 c3 c30">: Sorted list of numbers</span></p><p class="c1 c10"><span class="c11 c15 c3 c30">&nbsp; &nbsp;&quot;&quot;&quot;</span></p><p class="c1 c10"><span class="c11 c15 c3 c30">&nbsp; &nbsp;</span><span class="c11 c7 c3 c5">for </span><span class="c2 c11">i </span><span class="c11 c7 c3 c5">in </span><span class="c11 c3 c25">range</span><span class="c2 c11">(</span><span class="c11 c3 c25">len</span><span class="c2 c11">(num_list)-</span><span class="c11 c3 c20">1</span><span class="c11 c7 c3">, </span><span class="c11 c3 c20">0</span><span class="c11 c7 c3">, </span><span class="c2 c11">-</span><span class="c11 c3 c20">1</span><span class="c2 c11">):</span></p><p class="c1 c10"><span class="c2 c11">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c11 c7 c3 c5">for </span><span class="c2 c11">j </span><span class="c11 c7 c3 c5">in </span><span class="c11 c3 c25">range</span><span class="c2 c11">(i):</span></p><p class="c1 c10"><span class="c2 c11">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c11 c7 c3 c5">if </span><span class="c2 c11">num_list[j] &gt; num_list[j + </span><span class="c11 c3 c20">1</span><span class="c2 c11">]:</span></p><p class="c1 c10"><span class="c2 c11">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;temp = num_list[j]</span></p><p class="c1 c10"><span class="c2 c11">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;num_list[j] = num_list[j + </span><span class="c11 c3 c20">1</span><span class="c2 c11">]</span></p><p class="c1 c10"><span class="c2 c11">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;num_list[j + </span><span class="c11 c3 c20">1</span><span class="c2 c11">] = temp</span></p><p class="c1 c9 c10"><span class="c2 c11"></span></p><p class="c1 c10"><span class="c2 c11">my_list = [</span><span class="c11 c3 c20">4</span><span class="c11 c7 c3">, </span><span class="c11 c3 c20">266</span><span class="c11 c7 c3">, </span><span class="c11 c3 c20">9</span><span class="c11 c7 c3">, </span><span class="c11 c3 c20">24</span><span class="c11 c7 c3">, </span><span class="c11 c3 c20">44</span><span class="c11 c7 c3">, </span><span class="c11 c3 c20">54</span><span class="c11 c7 c3">, </span><span class="c11 c3 c20">41</span><span class="c11 c7 c3">, </span><span class="c11 c3 c20">89</span><span class="c11 c7 c3">, </span><span class="c11 c3 c20">20</span><span class="c2 c11">]</span></p><p class="c1 c10"><span class="c2 c11">bubble_sort(my_list)</span></p><p class="c1 c10"><span class="c11 c7 c3 c5">print</span><span class="c2 c11">(my_list)</span></p><p class="c1 c9 c10"><span class="c2 c11"></span></p><p class="c1 c10"><span class="c2 c11">&gt;&gt; [4, 9, 20, 24, 41, 44, 54, 89, 266]</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">In this algorithm </span><sup><a href="#cmnt52" id="cmnt_ref52">[az]</a></sup></p><h3 class="c1 c13" id="h.fyqh56v8nysv"><span>Sequential Search</span></h3><p class="c1"><span class="c3 c5">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">Search algorithms are used to find a number in a list of numbers. Sequential search is a simple search algorithm that checks each number in the list one by one to see if it matches the number it is looking for. A sequential search is the way you search a deck of cards for a specific card. You go one by one through each card, if the card is the card you are looking for, you stop. If you make it through the entire deck without finding the card, you know the card isn&rsquo;t there. Here is an example of sequential search:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3 c5">def sequential_search</span><span class="c3">(number_list, number):</span></p><p class="c1 c10"><span class="c3">&nbsp; &nbsp;</span><span class="c15 c3">&quot;&quot;&quot;</span></p><p class="c1 c10"><span class="c15 c3">&nbsp; &nbsp;:param number_list: List of integers.</span></p><p class="c1 c10"><span class="c15 c3">&nbsp; &nbsp;:param number: Integer to look for.</span></p><p class="c1 c10"><span class="c15 c3">&nbsp; &nbsp;:return: True if the number is found otherwise false.</span></p><p class="c1 c10"><span class="c15 c3">&nbsp; &nbsp;&quot;&quot;&quot;</span></p><p class="c1 c10"><span class="c15 c3">&nbsp; &nbsp;</span><span class="c3">found = False</span></p><p class="c1 c10"><span class="c3">&nbsp; &nbsp;</span><span class="c3 c5">for </span><span class="c3">i </span><span class="c3 c5">in </span><span class="c3">number_list:</span></p><p class="c1 c10"><span class="c3">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c3 c5">if &nbsp;</span><span class="c3">i == number:</span></p><p class="c1 c10"><span class="c3">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;found = True</span></p><p class="c1 c10"><span class="c3">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break</span></p><p class="c1 c10"><span class="c3">&nbsp; &nbsp;</span><span class="c3 c5">return </span><span class="c3">found</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3 c5">print </span><span class="c3">sequential_search(range(0, 100), 2)</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; True</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3 c5">print </span><span class="c3">sequential_search(range(0, 100), 202)</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; False</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">First &ldquo;found&rdquo; is set to false. Then we loop through every number in the list and check if it is equal to the number we are looking for. If it equal to the number we are looking for, we set found to True, exit our loop and return True. Otherwise, we continue to the next number in the list. If we get through the entire list and found has never been set to True. We return found which will still be set to False. </span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Sequential search is an O(n) algorithm. In the best case scenario, the number we are looking for could be the first number in the list, in which case our algorithm would take only one step. However, in the worst case scenario, the number is not in the list and we have to check every single number in the list, or n steps. If we have a million items in our list, worst case we have to search through a million items. If we have a billion items, worst case we have to search through a billion items. As our list grows, the worst case scenario for our algorithm grows by the size of our list, making this algorithm O(n). </span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.ju8sjaom4d1x"><span>Binary Search</span></h3><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Binary search is a logarithmic algorithm used to search for numbers in a list, but the numbers have to be ordered. &nbsp;Remember, in order for an algorithm to be logarithmic, it needs to either be dividing or multiplying to a solution. Any guesses how binary search works? Binary search works by continually cutting the list in half. The algorithm picks a number in the middle of a list, and looks at whether it&rsquo;s the right number. If it is the right number, the search is complete. If it&rsquo;s not the right number, the algorithm throws away half the list. If the number was too big, it throws away everything above the number it selected. If the number was too small, it throws away everything below the number it selected. </span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Image we have an ordered list from 1 to 10, and we want to search for the number 3. Our list looks like this:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[1,2,3,4,5,6,7,8,9,10]</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">Our algorithm would first pick the number 5 because it&rsquo;s in the middle of the list. Since 5 is not the number we are looking for, and 3 is smaller than five, our algorithm would throw out everything above 5. Now our list looks like this:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[1,2,3,4,5]</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">Our list would now pick the number three, since it&rsquo;s in the middle of the list. Since 3 is the number we are looking for, our algorithm would stop and return that the number 3 was found in our list. Notice our algorithm only took two steps to figure out three was in our list. If we searched through the list linearly, one by one, looking for the number three, it would takes us three steps. Here is an example of a binary search algorithm searching for the number 3:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3 c5">def binary_search</span><span class="c3">(number_list, number):</span></p><p class="c1"><span class="c3">&nbsp; &nbsp;</span><span class="c15 c3">&quot;&quot;&quot;Logarithmic binary search algorithm.</span></p><p class="c1"><span class="c15 c3">&nbsp; &nbsp;:param number_list: List of ordered integers.</span></p><p class="c1"><span class="c15 c3">&nbsp; &nbsp;:param number: Integer to search for in the passed in list.</span></p><p class="c1"><span class="c15 c3">&nbsp; &nbsp;:return: True if the number is found, otherwise False.</span></p><p class="c1"><span class="c15 c3">&nbsp; &nbsp;&quot;&quot;&quot;</span></p><p class="c1"><span class="c15 c3">&nbsp; &nbsp;</span><span class="c3">first = 0</span></p><p class="c1"><span class="c3">&nbsp; &nbsp;last = len(number_list)-1</span></p><p class="c1"><span class="c3">&nbsp; &nbsp;number_found = False</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp; &nbsp;</span><span class="c3 c5">while </span><span class="c3">first &lt;= last </span><span class="c3 c5">and not </span><span class="c3">number_found:</span></p><p class="c1"><span class="c3">&nbsp; &nbsp; &nbsp; &nbsp;middle = (first + last)/2</span></p><p class="c1"><span class="c3">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c3 c5">if </span><span class="c3">number_list[middle] == number:</span></p><p class="c1"><span class="c3">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;number_found = True</span></p><p class="c1"><span class="c3">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c3 c5">else</span><span class="c3">:</span></p><p class="c1"><span class="c3">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c3 c5">if </span><span class="c3">number &lt; number_list[middle]:</span></p><p class="c1"><span class="c3">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;last = middle - 1</span></p><p class="c1"><span class="c3">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c3 c5">else</span><span class="c3">:</span></p><p class="c1"><span class="c3">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;first = middle + 1</span></p><p class="c1"><span class="c3">&nbsp; &nbsp;</span><span class="c3 c5">return </span><span class="c3">number_found</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">binary_search([1,2,3,4,5,6,7,8,9,10], 3)</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">We use a while loop that continues as long as the variable first is not greater than or equal to the variable last, and the variable not_true is False. </span></p><p class="c0"><span class="c3">We calculate the middle index of the list by adding the first index of the list with the last index of the list and dividing them by two. The reason we subtract one from last is because the length of a list is calculated starting from one, whereas indexes start at zero.</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;We check to see if the middle number is the number we are looking for by looking up the middle number in our list with number_list[middle]. If the middle number &nbsp;is the number we are looking for, not_true is set to True, and we exit the loop. Otherwise, we check to see if the number we are looking for is bigger or smaller than the middle number. If our middle number is smaller than the number we are looking for, we change last to the middle index - 1, which on the next loop will split the list in half with everything smaller than our current middle number, whereas if our middle number is bigger than the number we are looking for, we change first to middle + 1, dividing the list in half so it contains everything bigger than our middle number.</span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.qzy5m4gvj0pj"><span>Recursion</span></h3><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Recursion is notorious as one of the toughest concepts for new programmers to grasp. If it is confusing to you at first, don&rsquo;t worry, it&rsquo;s confusing to everyone. Recursion is a method of solving problems by breaking the problem up into smaller and smaller pieces until it can be easily solved. This is achieved with a function that calls itself. Any problem that can be solved recursively can also be solved iteratively, however in certain cases, recursion offers a more elegant solution. </span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;A recursive algorithm must follow the three laws of recursion:</span></p><p class="c1"><span class="c3">&ldquo;1. A recursive algorithm must have a </span><span class="c3 c5">base case</span><span class="c3">.</span></p><p class="c1"><span class="c3">2. A recursive algorithm must change its state and move toward the base case.</span></p><p class="c1"><span class="c3">3. A recursive algorithm must call itself, recursively.&rdquo;</span></p><p class="c0"><span class="c3">Let&rsquo;s go over an example of a recursive function that has all three, a function to print out the lyrics to the popular children&rsquo;s song &ldquo;99 Bottles of Beer on the Wall&rdquo;19:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c10"><span class="c3 c5">def bottles_of_beer</span><span class="c3">(bob):</span></p><p class="c1 c10"><span class="c3">&nbsp; &nbsp;</span><span class="c15 c3">&quot;&quot;&quot; Use recursion to print the bottles of beer song.</span></p><p class="c1 c10"><span class="c15 c3">&nbsp; &nbsp;:param bob: Integer number of beers that start on the wall.</span></p><p class="c1 c10"><span class="c15 c3">&nbsp; &nbsp;&quot;&quot;&quot;</span></p><p class="c1 c10"><span class="c15 c3">&nbsp; &nbsp;</span><span class="c3 c5">if </span><span class="c3">bob &lt; 1:</span></p><p class="c1 c10"><span class="c3">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c3 c5">print </span><span class="c3">&quot;No more bottles of beer on the wall. No more bottles of beer.&quot;</span></p><p class="c1 c10"><span class="c3">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c3 c5">return</span></p><p class="c1 c10"><span class="c3 c5">&nbsp; &nbsp;</span><span class="c3">tmp = bob</span></p><p class="c1 c10"><span class="c3">&nbsp; &nbsp;bob -= 1</span></p><p class="c1 c10"><span class="c3">&nbsp; </span><span class="c3 c5">print </span><span class="c3">&quot;{} bottles of beer on the wall. {} bottles of beer. Take one down, pass it around, {} bottles of beer on the wall.&quot;.format(tmp, tmp, bob)</span></p><p class="c1 c10"><span class="c3">&nbsp; &nbsp;bottles_of_beer(bob)</span></p><p class="c1 c9 c10"><span class="c3"></span></p><p class="c1 c10"><span class="c3">bottles_of_beer(99)</span></p><p class="c1 c10"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p><p class="c1 c10"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; 99 bottles of beer on the wall. 99 bottles of beer. Take one down, pass it around, 98 bottles of beer on the wall.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p><p class="c1 c10"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; 98 bottles of beer on the wall. 98 bottles of beer. Take one down, pass it around, 97 bottles of beer on the wall.</span></p><p class="c1 c10"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&hellip;</span></p><p class="c1 c10"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt;No more bottles of beer on the wall. No more bottles of beer.</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">In this example, the base case is:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c15 c3">&nbsp; &nbsp;</span><span class="c3 c5">if </span><span class="c3">bob &lt; 1:</span></p><p class="c1"><span class="c3">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; </span><span class="c3 c5">print </span><span class="c3">&quot;No more bottles of beer on the wall. No more bottles of beer.&quot;</span></p><p class="c1"><span class="c3">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; </span><span class="c3 c5">return</span></p><p class="c1 c9"><span class="c3 c5"></span></p><p class="c1"><span class="c3">The base case of a recursive algorithm is what finally stops the algorithm from running. If you have a recursive algorithm without a base case, it will continue to call itself forever, and you will get a runtime error saying the maximum recursion depth has been exceeded. The line:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c0"><span class="c3">bob -= 1</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">satisfies our second rule that we must move toward our base case. In our example, we passed in the number &ldquo;99&rdquo; to our function as the parameter bob. Since our base case is bob being less than 1, and because bob starts at 99, without this line we will never reach our base case, which will also give us a maximum recursion depth runtime error. </span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Our final rule is satisfied with :</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c0"><span class="c3">bottles_of_beer(bob)</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">With this line, we are calling our function. The function will get called again, but with one important difference. Instead of passing in 99 like the first time the function was called, this time 98 will be passed in, because of rule number two. The third the function is called 97 will be called. This will continue to happen until eventually bob is equal to 0, and we hit our base case. At that point we print &quot;No more bottles of beer on the wall. No more bottles of beer.&quot; and return, signaling the algorithm to stop. </span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Let&rsquo;s go over one more recursive algorithm. Say you are given the following problem:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c10"><span class="c11 c3">Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.</span></p><p class="c1 c10"><span class="c11 c3">For example:</span></p><p class="c1 c10"><span class="c11 c3">Given num = 38, the process is like: 3 + 8 = 11, 1 + 1 = 2. Since 2 has only one digit, return it.</span></p><p class="c1 c9"><span></span></p><p class="c1 c98"><span class="c11 c3 c33 c22">One way we can solve this problem is using recursion</span></p><p class="c1 c39 c10"><span class="c11 c7 c3 c5">def </span><span class="c2 c11 c5">add_digits</span><span class="c2 c11">(number):</span></p><p class="c1 c39 c10"><span class="c2 c11">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c11 c15 c3 c30">&quot;&quot;&quot;</span></p><p class="c1 c10 c39"><span class="c11 c15 c3 c30">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c11 c15 c3 c30 c5">:param</span><span class="c11 c15 c3 c30">&nbsp;number: Int</span></p><p class="c1 c39 c10"><span class="c11 c15 c3 c30">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c11 c15 c3 c30 c5">:return</span><span class="c11 c15 c3 c30">: Single digit int</span></p><p class="c1 c39 c10"><span class="c11 c15 c3 c30">&nbsp; &nbsp; &nbsp; &nbsp;&quot;&quot;&quot;</span></p><p class="c1 c39 c10"><span class="c11 c15 c3 c30">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c2 c11">number = </span><span class="c11 c3 c25">str</span><span class="c2 c11">(number)</span></p><p class="c1 c39 c10"><span class="c2 c11">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c11 c7 c3 c5">if </span><span class="c11 c3 c25">len</span><span class="c2 c11">(number) == </span><span class="c11 c3 c20">1</span><span class="c2 c11">:</span></p><p class="c1 c39 c10"><span class="c2 c11">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c11 c7 c3 c5">return </span><span class="c11 c3 c25">int</span><span class="c2 c11">(number)</span></p><p class="c1 c39 c10"><span class="c2 c11">&nbsp; &nbsp; &nbsp; &nbsp;the_sum = </span><span class="c11 c3 c20">0</span></p><p class="c1 c39 c10"><span class="c11 c3 c20">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c11 c7 c3 c5">for </span><span class="c2 c11">c </span><span class="c11 c7 c3 c5">in </span><span class="c2 c11">number:</span></p><p class="c1 c39 c10"><span class="c2 c11">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;the_sum += </span><span class="c11 c3 c25">int</span><span class="c2 c11">(c)</span></p><p class="c1 c39 c10"><span class="c2 c11">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c11 c7 c3 c5">return </span><span class="c2 c11">add_digits(the_sum)</span></p><p class="c1 c39"><span class="c11 c7 c3 c5">print </span><span class="c2 c11">add_digits(</span><span class="c11 c3 c20">99</span><span class="c2 c11">)</span></p><p class="c1 c39"><span class="c2 c11">&gt;&gt; 9</span></p><p class="c1"><span class="c3">In this example, our function accepts a number as a parameter, calculates its sum and calls itself until it hits the base case which is a number with only one digit. If we pass in the number ninety nine, it has two digits so it fails the base case. We then calculate that the_sum equals 18, so we call add_digits again and pass in 18. &nbsp;Once again 18 does not pass our base case so we calculate the sum of the digits which this time is 9, and call add_digits with 9 as a parameter. The third time around, our number is 9 and since it&rsquo;s only one digit, &nbsp;it does satisfy our base case and we return the answer. </span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.bd5n41gpj60j"><span>Abstract Data Types</span></h3><p class="c1"><span class="c3 c5">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">When learning about data structures, you will come across the term abstract data type. I remember what an abstract data type is by thinking of the relationship between an abstract data type and a data structure as similar (although not exactly the same) as the relationship between a class and an object. </span></p><p class="c1"><span class="c3">If you want to model an orange in object-oriented programming, a class represents the idea of an orange, whereas the object represents the actual orange. Similarly, an abstract data type represents the idea of a certain type of data structure. An example of an abstract data type is a list. A list is an abstract data type that can be implemented several ways with data structures such as an array, or a linked list. &nbsp;</span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.23539d9hay73"><span>Nodes</span></h3><p class="c1"><span class="c3">Node is a term used frequently in Computer Science. You can think of a node as a &nbsp;point on a graph. The internet is made up of routers that communicate with each other. Each router is a node in the network. Nodes are used in several data structures, including linked lists, trees and graphs. </span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.41ols0h1i02n"><span>Stacks</span></h3><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;A stack is a last in first out data structure. This is best envisioned as a stack of dishes. Say you stack five dishes on top of each other. In order to get to the last dish in the stack, you have to remove all of the other dishes. This is how a stack data structure works. You put data into a stack. Every piece of data is like a dish, and you can only access the data by pulling out the data at the top of the stack. Here is an example of a stack implemented in Python:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c10"><span class="c3">class Stack:</span></p><p class="c1 c10"><span class="c3">&nbsp; &nbsp; &nbsp;def __init__(self):</span></p><p class="c1 c10"><span class="c3">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;self.items = []</span></p><p class="c1 c9 c10"><span class="c3"></span></p><p class="c1 c10"><span class="c3">&nbsp; &nbsp; &nbsp;def isEmpty(self):</span></p><p class="c1 c10"><span class="c3">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return self.items == []</span></p><p class="c1 c9 c10"><span class="c3"></span></p><p class="c1 c10"><span class="c3">&nbsp; &nbsp; &nbsp;def push(self, item):</span></p><p class="c1 c10"><span class="c3">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;self.items.append(item)</span></p><p class="c1 c9 c10"><span class="c3"></span></p><p class="c1 c10"><span class="c3">&nbsp; &nbsp; &nbsp;def pop(self):</span></p><p class="c1 c10"><span class="c3">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return self.items.pop()</span></p><p class="c1 c9 c10"><span class="c3"></span></p><p class="c1 c10"><span class="c3">&nbsp; &nbsp; &nbsp;def peek(self):</span></p><p class="c1 c10"><span class="c3">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return self.items[len(self.items)-1]</span></p><p class="c1 c9 c10"><span class="c3"></span></p><p class="c1 c10"><span class="c3">&nbsp; &nbsp; &nbsp;def size(self):</span></p><p class="c1 c10"><span class="c3">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return len(self.items)</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">The two most important methods in the definition are push and pop. Push lets you put data on top of the stack, and pop lets you take it off the stack. </span></p><p class="c1"><span class="c3">So what are stacks used for? Well first of all, stacks are important for understanding recursion. Recursion is a fundamental part of programming we go over in a later section. Most new programmers struggle with recursion, but the key to understanding recursion is to deeply understand how a stack works. </span></p><p class="c1"><span class="c3">Furthermore, stacks are used to reverse things. Whatever you put on a stack comes out in reverse order when you take it off. For example, let&rsquo;s say you want to reverse a string. We could reverse the string by first putting in on a stack, and then taking it off, like this:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c10"><span class="c3">from collections import stack as s</span></p><p class="c1 c9 c10"><span class="c3"></span></p><p class="c1 c10"><span class="c3">my_string = &ldquo;Hello&rdquo;</span></p><p class="c1 c10"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;stack = s()</span></p><p class="c1 c9 c10"><span class="c3"></span></p><p class="c1 c10"><span class="c3">for c in my_string:</span></p><p class="c1 c10"><span class="c3">&nbsp;stack.push(c)</span></p><p class="c1 c9 c10"><span class="c3"></span></p><p class="c1 c10"><span class="c3">new_string = &ldquo;&rdquo;</span></p><p class="c1 c10"><span class="c3">for c in stack:</span></p><p class="c1 c10"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new_string += c</span></p><p class="c1 c10"><span class="c3">print new_string</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">In this example we went through each character in the word &ldquo;Hello&rdquo;, and put it on our stack. Then we we iterated through our stack, and took everything we just put on the stack, off of it, and saved the order in the variable new_string. By the time we get to the last line, our word is reversed and our program prints &ldquo;olleH&rdquo;.</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;I&rsquo;m going to share another example of using a stack. It&rsquo;s a rare example of a question I&rsquo;ve been asked to solve in an interview, and actually have used on the job. The problem is, write a program that tests a string for balanced parentheses. So, for example, &ldquo;(hello)&rdquo; would pass, but &ldquo;(hello&rdquo; would fail. &ldquo;()()()&rdquo; would pass, but &ldquo;()(&rdquo; would fail. This looks easy at first, until we get something like this &ldquo;((()((()((()((((()))))&rdquo;.</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;How are you supposed to keep track of all the parenthesis? The key to solving this problem is to use a stack. Every time we come across an open paren, we put it on a stack. If we come across a closed paren, we pull an open paren off the stack. </span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c10"><span class="c11 c7 c3 c5">def </span><span class="c2 c11 c5">balanced_paren</span><span class="c2 c11">(expression):</span></p><p class="c1 c10"><span class="c2 c11">&nbsp; &nbsp;stack = []</span></p><p class="c1 c10"><span class="c2 c11">&nbsp; &nbsp;</span><span class="c11 c7 c3 c5">for </span><span class="c2 c11">c </span><span class="c11 c7 c3 c5">in </span><span class="c2 c11">expression:</span></p><p class="c1 c10"><span class="c2 c11">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c11 c7 c3 c5">if </span><span class="c2 c11">c == </span><span class="c11 c40 c3">&#39;(&#39;</span><span class="c2 c11">:</span></p><p class="c1 c10"><span class="c2 c11">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;stack.append(c)</span></p><p class="c1 c10"><span class="c2 c11">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c11 c7 c3 c5">elif </span><span class="c2 c11">c == </span><span class="c11 c40 c3">&#39;)&#39;</span><span class="c2 c11">:</span></p><p class="c1 c10"><span class="c2 c11">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c11 c7 c3 c5">if </span><span class="c11 c3 c25">len</span><span class="c2 c11">(stack) &lt; </span><span class="c11 c3 c20">1</span><span class="c2 c11">:</span></p><p class="c1 c10"><span class="c2 c11">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c11 c7 c3 c5">return </span><span class="c11 c3 c25">False</span></p><p class="c1 c10"><span class="c11 c3 c25">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c2 c11">stack.pop()</span></p><p class="c1 c10"><span class="c2 c11">&nbsp; &nbsp;</span><span class="c11 c7 c3 c5">if </span><span class="c11 c3 c25">len</span><span class="c2 c11">(stack) == </span><span class="c11 c3 c20">0</span><span class="c2 c11">:</span></p><p class="c1 c10"><span class="c2 c11">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c11 c7 c3 c5">return </span><span class="c11 c3 c25">True</span></p><p class="c1 c10"><span class="c11 c3 c25">&nbsp; &nbsp;</span><span class="c11 c7 c3 c5">return </span><span class="c11 c3 c25">False</span></p><p class="c1 c9"><span class="c11 c3 c25"></span></p><p class="c1"><span class="c3">If the parenthesis are balanced, our stack will be empty after our loop, and we can return &ldquo;True&rdquo;. &nbsp;One thing we need to watch out for, if the parenthesis are unbalanced, we will try to pop from an empty stack which will cause an error. That is why when we come across a closed paren, we have to first make sure the stack is not empty before we pop it off the stack. If we come across a closed paren, and the stack is empty, we know the parenthesis are not balanced and we can return &ldquo;False&rdquo;. If at the end of the loop there are still open parenthesis on the stack, we can also return &ldquo;False&rdquo;.</span></p><p class="c1"><span class="c3">&nbsp;</span></p><h3 class="c1 c13" id="h.bpsl2a6lud5c"><span>Linked Lists</span></h3><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;A linked list is made up of a series of nodes, with each node pointing to the next node in the list. My friend Steve gave a great metaphor for thinking about linked lists. Imagine you are in the Mafia and want to give orders in such a way that none knows who you are. You could set up a structure where you give an order &nbsp;anonymously to the next person down the chain of command. You know who they are but they don&rsquo;t know you, they only know the next person they should give the order to. The person they give the next order to doesn&rsquo;t know them, but only knows the next person to receive the order. This chain of information is what a singly linked list is. In a singly linked list, all of the nodes in the list only know about the next node. They don&rsquo;t keep track of the node behind them. You can get to every piece of data in a linked list by starting at the head of the list, and moving one by one to each next node. A doubly linked list is the same thing except each node keeps track of the node behind it, in addition to keeping track of the next node. A linked list can also be ordered or unordered. In this section, we will implement an unordered singly and doubly linked list:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c10"><span class="c3">class Node:</span></p><p class="c1 c10"><span class="c3">&nbsp; &nbsp;&quot;&quot;&quot;Class representing one node in a linked list.&quot;&quot;&quot;</span></p><p class="c1 c10"><span class="c3">&nbsp; &nbsp;def __init__(self, data):</span></p><p class="c1 c10"><span class="c3">&nbsp; &nbsp; &nbsp; &nbsp;self.data = Node(data)</span></p><p class="c1 c10"><span class="c3">&nbsp; &nbsp; &nbsp; &nbsp;self.next = None</span></p><p class="c1 c9 c10"><span class="c3"></span></p><p class="c1 c10"><span class="c3">class LinkedList:</span></p><p class="c1 c10"><span class="c3">&nbsp; &nbsp;&quot;&quot;&quot;Class representing a linked list data structure&quot;&quot;&quot;</span></p><p class="c1 c10"><span class="c3">&nbsp; &nbsp;def __init__(self, head):</span></p><p class="c1 c10"><span class="c3">&nbsp; &nbsp; &nbsp; &nbsp;self.head = head</span></p><p class="c1 c9 c10"><span class="c3"></span></p><p class="c1 c10"><span class="c3">&nbsp; &nbsp;def add(self, data):</span></p><p class="c1 c10"><span class="c3">&nbsp; &nbsp; &nbsp; &nbsp;&quot;&quot;&quot;Add a new node to the linked list.&quot;&quot;&quot;</span></p><p class="c1 c10"><span class="c3">&nbsp; &nbsp; &nbsp; &nbsp;previous_head = self.head</span></p><p class="c1 c10"><span class="c3">&nbsp; &nbsp; &nbsp; &nbsp;self.head = Node(data)</span></p><p class="c1 c10"><span class="c3">&nbsp; &nbsp; &nbsp; &nbsp;self.head.next = previous_head</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">Our linked list class is simple. It stores the head of the linked list and has a method called &ldquo;add&rdquo; to add a new node to the list. Since the list is unordered, we don&rsquo;t care where we put the next node, so it&rsquo;s easiest to put it at the head of the list. The method add stores the current head in the previous_head variable, so we don&rsquo;t lose track of it, creates a new node with the passed in data, and sets the new node as the head of the list. The node that used to be the head, is then set as the next node after the new head. Now we can create a linked list and add data to it:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;linked_list = LinkedList()</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;linked_list.add(1)</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;linked_list.add(2)</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;linked_list.add(3)</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">To get the data from our linked list, we start with &nbsp;the head, and visit every node until we hit a next node that equals none. </span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;node = linked_list.head</span></p><p class="c1 c10"><span class="c3">while node:</span></p><p class="c1 c10"><span class="c3">&nbsp; &nbsp; print node.data</span></p><p class="c1 c10"><span class="c3">&nbsp; &nbsp; node = node.next</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&gt;&gt; 3</span></p><p class="c1 c10"><span class="c3">&gt;&gt; 2</span></p><p class="c1 c10"><span class="c3">&gt;&gt; 1</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">We can change our singly linked list to a doubly linked list by keeping changing our node class to keep track of the node behind it. </span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c10"><span class="c11 c7 c3 c5">class </span><span class="c2 c11 c5">Node</span><span class="c2 c11">:</span></p><p class="c1 c10"><span class="c2 c11">&nbsp; &nbsp;</span><span class="c11 c15 c3 c30">&quot;&quot;&quot;Class representing one node in a linked list.&quot;&quot;&quot;</span></p><p class="c1 c10"><span class="c11 c15 c3 c30">&nbsp; &nbsp;</span><span class="c11 c7 c3 c5">def </span><span class="c29 c11 c3">__init__</span><span class="c2 c11">(</span><span class="c11 c3 c8">self</span><span class="c11 c7 c3">, </span><span class="c2 c11">data):</span></p><p class="c1 c10"><span class="c2 c11">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c11 c3 c8">self</span><span class="c2 c11">.data = data</span></p><p class="c1 c10"><span class="c2 c11">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c11 c3 c8">self</span><span class="c2 c11">.</span><span class="c29 c11 c3">next </span><span class="c2 c11">= </span><span class="c11 c3 c25">None</span></p><p class="c1 c10"><span class="c11 c3 c25">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c11 c3 c8">self</span><span class="c2 c11">.previous = </span><span class="c11 c3 c25">None</span></p><p class="c1 c9"><span class="c11 c3 c25"></span></p><p class="c1 c10"><span class="c11 c7 c3 c5">class </span><span class="c2 c11 c5">LinkedList</span><span class="c2 c11">:</span></p><p class="c1 c10"><span class="c2 c11">&nbsp; &nbsp;</span><span class="c11 c15 c3 c30">&quot;&quot;&quot;Class representing a linked list data structure&quot;&quot;&quot;</span></p><p class="c1 c10"><span class="c11 c15 c3 c30">&nbsp; &nbsp;</span><span class="c11 c7 c3 c5">def </span><span class="c29 c11 c3">__init__</span><span class="c2 c11">(</span><span class="c11 c3 c8">self</span><span class="c11 c7 c3">, </span><span class="c2 c11">data):</span></p><p class="c1 c10"><span class="c2 c11">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c11 c3 c8">self</span><span class="c2 c11">.head = Node(data)</span></p><p class="c1 c9 c10"><span class="c2 c11"></span></p><p class="c1 c10"><span class="c2 c11">&nbsp; &nbsp;</span><span class="c11 c7 c3 c5">def </span><span class="c2 c11 c5">add</span><span class="c2 c11">(</span><span class="c11 c3 c8">self</span><span class="c11 c7 c3">, </span><span class="c2 c11">data):</span></p><p class="c1 c10"><span class="c2 c11">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c11 c15 c3 c30">&quot;&quot;&quot;Add a new node to the linked list.&quot;&quot;&quot;</span></p><p class="c1 c10"><span class="c11 c15 c3 c30">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c2 c11">previous_head = </span><span class="c11 c3 c8">self</span><span class="c2 c11">.head</span></p><p class="c1 c10"><span class="c2 c11">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c11 c3 c8">self</span><span class="c2 c11">.head = Node(data)</span></p><p class="c1 c10"><span class="c2 c11">&nbsp; &nbsp; &nbsp; &nbsp;previous_head.previous = </span><span class="c11 c3 c8">self</span><span class="c2 c11">.head</span></p><p class="c1 c10"><span class="c2 c11">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c11 c3 c8">self</span><span class="c2 c11">.head.</span><span class="c29 c11 c3">next </span><span class="c2 c11">= previous_head</span></p><p class="c1 c9"><span class="c2 c11"></span></p><p class="c1"><span class="c11 c3">Our new linked list is the same as our previous one, except now every node knows the node in front and back of it. </span></p><p class="c1 c9"><span class="c11 c3"></span></p><h3 class="c1 c13" id="h.rakpjxvfvhqa"><span>Arrays</span></h3><p class="c1"><span class="c3 c5">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">An array is an implementation of the list abstract data type. &nbsp;Every piece of data in an array has to be the same type. So for example, you can have an array made up of strings or ints but you cannot have an array made up of both. Python&rsquo;s built in list data structure is implemented internally in C as an array of pointers. </span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.9t8mghwa8suf"><span>Binary Trees</span></h3><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;A tree is another data structure that gets its name from looking like, you guessed it, a tree. A great example of a tree data structure is the file system on your computer, which is implemented using a tree. There are many different kinds of trees such as red and black trees, AVL trees and binary trees. In this section, we will build a binary tree.</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;A binary tree is made up of nodes containing data. Each node can have a left child and a right child. In the same way a linked list points to the next element in the list, a node can point to two other nodes called the left and right child. Like a doubly linked list, the left and right child keep track of their parent. Here is an example of a binary tree implemented in Python:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c10"><span class="c3 c5">class Node</span><span class="c3">:</span></p><p class="c1 c10"><span class="c3">&nbsp; &nbsp;</span><span class="c15 c3">&quot;&quot;&quot;Binary tree node.&quot;&quot;&quot;</span></p><p class="c1 c10"><span class="c15 c3">&nbsp; &nbsp;</span><span class="c3 c5">def </span><span class="c3">__init__(self, value):</span></p><p class="c1 c10"><span class="c3">&nbsp; &nbsp; &nbsp; &nbsp;self.value = value</span></p><p class="c1 c10"><span class="c3">&nbsp; &nbsp; &nbsp; &nbsp;self.left_child = None</span></p><p class="c1 c10"><span class="c3">&nbsp; &nbsp; &nbsp; &nbsp;self.right_child = None</span></p><p class="c1 c9 c10"><span class="c3"></span></p><p class="c1 c10"><span class="c3 c5">class BinaryTree</span><span class="c3">:</span></p><p class="c1 c10"><span class="c3">&nbsp; &nbsp;</span><span class="c15 c3">&quot;&quot;&quot;This class represents a binary tree data structure.&quot;&quot;&quot;</span></p><p class="c1 c10"><span class="c15 c3">&nbsp; &nbsp;</span><span class="c3 c5">def </span><span class="c3">__init__(self, root):</span></p><p class="c1 c10"><span class="c3">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c15 c3">&quot;&quot;&quot;</span></p><p class="c1 c10"><span class="c15 c3">&nbsp; &nbsp; &nbsp; &nbsp;:param root: Binary tree node.</span></p><p class="c1 c10"><span class="c15 c3">&nbsp; &nbsp; &nbsp; &nbsp;&quot;&quot;&quot;</span></p><p class="c1 c10"><span class="c15 c3">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c3">self.root = root</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">Our binary tree is simply made up of a root. </span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.luujnwxy30gf"><span>Breadth First &amp; Depth First Search </span></h3><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;If we want to visit every node in a binary tree, there are two search algorithms we can use: breadth first search, and depth first search. If you think of a tree of being made up of rows and columns, in a breadth first search we visit each row one by one, whereas in depth first search we visit each column one by one. We will use the binary tree from the previous example to create our tree to traverse:</span></p><p class="c1 c9 c10"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tree = BinaryTree(&ldquo;a&rdquo;)</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tree.left = TreeNode(&ldquo;b&rdquo;)</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tree.right = TreeNode(&ldquo;c&rdquo;)</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tree.left.left = TreeNode(&ldquo;d&rdquo;)</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tree.right.right = TreeNode(&ldquo;e&rdquo;)</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">Now we can write a function that takes a tree as a parameter and does a breadth first search of the tree, printing out the value of each node it visits:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c10"><span class="c11 c7 c3 c5">def </span><span class="c2 c11 c5">breadth_first</span><span class="c2 c11">(tree):</span></p><p class="c1 c10"><span class="c2 c11">&nbsp; &nbsp;</span><span class="c11 c15 c3 c30">&quot;&quot;&quot; Breadth first search of binary tree print out each node.</span></p><p class="c1 c10"><span class="c11 c15 c3 c30">&nbsp; &nbsp;</span><span class="c11 c15 c3 c30 c5">:param</span><span class="c11 c15 c3 c30">&nbsp;root: BinaryTree</span></p><p class="c1 c10"><span class="c11 c15 c3 c30">&nbsp; &nbsp;&quot;&quot;&quot;</span></p><p class="c1 c10"><span class="c11 c15 c3 c30">&nbsp; &nbsp;</span><span class="c2 c11">current_level = [root]</span></p><p class="c1 c10"><span class="c2 c11">&nbsp; &nbsp;next_level = []</span></p><p class="c1 c10"><span class="c2 c11">&nbsp; &nbsp;</span><span class="c11 c7 c3 c5">while </span><span class="c2 c11">current_level:</span></p><p class="c1 c10"><span class="c2 c11">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c11 c7 c3 c5">for </span><span class="c2 c11">node </span><span class="c11 c7 c3 c5">in </span><span class="c2 c11">current_level:</span></p><p class="c1 c10"><span class="c2 c11">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c11 c7 c3 c5">print </span><span class="c2 c11">node.val</span></p><p class="c1 c10"><span class="c2 c11">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c11 c7 c3 c5">if </span><span class="c2 c11">node.left_child:</span></p><p class="c1 c10"><span class="c2 c11">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;next_level.append(node.left_child)</span></p><p class="c1 c10"><span class="c2 c11">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c11 c7 c3 c5">if </span><span class="c2 c11">node.right_child:</span></p><p class="c1 c10"><span class="c2 c11">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;next_level.append(node.right_child)</span></p><p class="c1 c10"><span class="c2 c11">&nbsp; &nbsp; &nbsp; &nbsp;current_level = next_level</span></p><p class="c1 c10"><span class="c2 c11">&nbsp; &nbsp; &nbsp; &nbsp;next_level = []</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">We use the list current_level to keep track of all the nodes in the level of the tree we are currently in. When there is no more current level, our algorithm stops. Our tree looks like this:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a</span></p><p class="c0"><span class="c3">/ \</span></p><p class="c1"><span class="c3">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;b &nbsp; &nbsp;c</span></p><p class="c1"><span class="c3">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; / \ </span></p><p class="c1"><span class="c3">&nbsp; &nbsp; &nbsp; &nbsp; d &nbsp; e</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&ldquo;current_level&rdquo; will start as [a], become [b, c], become [d, e] and finally become an empty list [] at which point our algorithm is finished. We are able to do this by keeping track of the next level of nodes in our list next_level. </span></p><p class="c0"><span class="c3">Before our while loop, we add the root of our tree to current_level, then tell our while loop to continue, as long as current_level isn&rsquo;t empty. In our while loop, we iterate through every node in current_level, printing out each node. Then we check if the node has any children. If it does, we add those children to our next_level list. At the end of our for loop, we switch the two lists, so the current_level list is set to the next_level list, and the next_level list becomes empty. This is what allows us to move from one level to the next. Eventually, when we reach the last level, the nodes will not have any children, next_level will be empty, current_level will be set to next_level, and because it&rsquo;s empty the algorithm will stop. </span></p><p class="c0"><span class="c3">A depth first search searches the tree vertically instead of traversing across it horizontally. Our tree from the previous example would be searched &ldquo;a&rdquo;, &ldquo;b&rdquo;, &ldquo;d&rdquo;, &ldquo;e&rdquo;, &ldquo;c&rdquo;. We can implement depth first search using recursion:</span></p><p class="c0 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.3m5ekm4v8y4p"><span>Hash Tables</span><sup><a href="#cmnt53" id="cmnt_ref53">[ba]</a></sup></h3><p class="c1"><span class="c3 c5">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">In the chapter Containers, we covered Python&rsquo;s built in dictionary data type. Dictionaries are helpful because they can store keys and values and are incredibly fast at getting and setting data. To recap, dictionaries map keys to values. For instance you could add the key &ldquo;super_computer&rdquo; to a dictionary with the value &ldquo;Watson&rdquo; with the following code:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;my_dictionary = {}</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;my_dictionary[&ldquo;super_computer&rdquo;] = &ldquo;Watson&rdquo;</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">Now we can retrieve the key &ldquo;super_computer&rdquo; &nbsp;in with:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c0"><span class="c3">print my_dictionary[&ldquo;super_computer&rdquo;] </span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">The amazing thing about dictionaries is they can set and get data in constant time. It doesn&rsquo;t matter how many rows of data we have in our dictionary. We could have one billion rows, and still add and retrieve the value for &ldquo;super_computer&rdquo; to our dictionary in O(1) time. </span></p><p class="c0"><span class="c3">Internally, Python uses a hash table to implement its dictionary. A hash table is a data structure that &nbsp;uses a list and a hash function to store data in &nbsp;O(1) time. When you add a value to a hash table, it uses a hash function to come up with an index in the list to store the data. When you retrieve data from a hash table, it uses the same hash function to find the index so it can retrieve it from the list. In this example, our hash table is only going to store numbers. The hash function will return the result of the number modulo eleven. So for example, our hash function for one would return one, so we store the number one at index one in our list. Our hash function for the number five would return five, and so we would store the number five at index five in our list. Here is an example of a hash table:</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p><p class="c1"><span class="c11 c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c11 c7 c3 c5">class </span><span class="c2 c11 c5">HashTable</span><span class="c2 c11">:</span></p><p class="c1 c10"><span class="c2 c11">&nbsp; &nbsp;</span><span class="c11 c15 c3 c30">&quot;&quot;&quot;Hash table data structure&quot;&quot;&quot;</span></p><p class="c1 c10"><span class="c11 c15 c3 c30">&nbsp; &nbsp;</span><span class="c11 c7 c3 c5">def </span><span class="c29 c11 c3">__init__</span><span class="c2 c11">(</span><span class="c11 c3 c8">self</span><span class="c2 c11">):</span></p><p class="c1 c10"><span class="c2 c11">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c11 c3 c8">self</span><span class="c2 c11">.list = [</span><span class="c11 c3 c25">None</span><span class="c2 c11">] * </span><span class="c11 c3 c20">11</span></p><p class="c1 c9 c10"><span class="c11 c3 c20"></span></p><p class="c1 c10"><span class="c11 c3 c20">&nbsp; &nbsp;</span><span class="c11 c3 c25">@</span><span class="c11 c3 c54">staticmethod</span></p><p class="c1 c10"><span class="c11 c54 c3">&nbsp; &nbsp;</span><span class="c11 c7 c3 c5">def </span><span class="c2 c11 c5">hash</span><span class="c2 c11">(n):</span></p><p class="c1 c10"><span class="c2 c11">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c11 c15 c3 c30">&quot;&quot;&quot;</span></p><p class="c1 c10"><span class="c11 c15 c3 c30">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c11 c15 c3 c30 c5">:param</span><span class="c11 c15 c3 c30">&nbsp;n: int</span></p><p class="c1 c10"><span class="c11 c15 c3 c30">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c11 c15 c3 c30 c5">:return</span><span class="c11 c15 c3 c30">: return index in list to store number.</span></p><p class="c1 c10"><span class="c11 c15 c3 c30">&nbsp; &nbsp; &nbsp; &nbsp;&quot;&quot;&quot;</span></p><p class="c1 c10"><span class="c11 c15 c3 c30">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c11 c7 c3 c5">return </span><span class="c2 c11">n % </span><span class="c11 c3 c20">11</span></p><p class="c1 c9 c10"><span class="c11 c3 c20"></span></p><p class="c1 c10"><span class="c11 c3 c20">&nbsp; &nbsp;</span><span class="c11 c7 c3 c5">def </span><span class="c2 c11 c5">set</span><span class="c2 c11">(</span><span class="c11 c3 c8">self</span><span class="c11 c7 c3">, </span><span class="c2 c11">n</span><span class="c11 c7 c3">, </span><span class="c2 c11">v):</span></p><p class="c1 c10"><span class="c2 c11">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c11 c15 c3 c30">&quot;&quot;&quot;</span></p><p class="c1 c10"><span class="c11 c15 c3 c30">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c11 c15 c3 c30 c5">:param</span><span class="c11 c15 c3 c30">&nbsp;n: int</span></p><p class="c1 c10"><span class="c11 c15 c3 c30">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c11 c15 c3 c30 c5">:param</span><span class="c11 c15 c3 c30">&nbsp;v: can be any type.</span></p><p class="c1 c10"><span class="c11 c15 c3 c30">&nbsp; &nbsp; &nbsp; &nbsp;&quot;&quot;&quot;</span></p><p class="c1 c10"><span class="c11 c15 c3 c30">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c11 c3 c8">self</span><span class="c2 c11">.list[</span><span class="c11 c3 c8">self</span><span class="c2 c11">.hash(n)] = v</span></p><p class="c1 c9"><span class="c2 c11"></span></p><p class="c1 c10"><span class="c2 c11">&nbsp; &nbsp;</span><span class="c11 c7 c3 c5">def </span><span class="c2 c11 c5">get</span><span class="c2 c11">(</span><span class="c11 c3 c8">self</span><span class="c11 c7 c3">, </span><span class="c2 c11">n):</span></p><p class="c1 c10"><span class="c2 c11">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c11 c15 c3 c30">&quot;&quot;&quot;</span></p><p class="c1 c10"><span class="c11 c15 c3 c30">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c11 c15 c3 c30 c5">:param</span><span class="c11 c15 c3 c30">&nbsp;n: int</span></p><p class="c1 c10"><span class="c11 c15 c3 c30">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c11 c15 c3 c30 c5">:return</span><span class="c11 c15 c3 c30">: int value from list</span></p><p class="c1 c10"><span class="c11 c15 c3 c30">&nbsp; &nbsp; &nbsp; &nbsp;&quot;&quot;&quot;</span></p><p class="c1 c10"><span class="c11 c15 c3 c30">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c11 c7 c3 c5">return </span><span class="c11 c3 c8">self</span><span class="c2 c11">.list[</span><span class="c11 c3 c25">hash</span><span class="c2 c11">(n)]</span></p><p class="c1 c9 c10"><span class="c2 c11"></span></p><p class="c1 c10"><span class="c2 c11">hash_table = </span><span class="c2 c11 c5">HashTable</span><span class="c2 c11">()</span></p><p class="c1 c10"><span class="c2 c11">hash_table.set(</span><span class="c11 c3 c20">1</span><span class="c11 c7 c3">, </span><span class="c11 c40 c3">&#39;Disrupted&#39;</span><span class="c2 c11">)</span></p><p class="c1 c10"><span class="c2 c11">hash_table.set(</span><span class="c11 c3 c20">5</span><span class="c11 c7 c3">, </span><span class="c11 c40 c3">&#39;HubSpot&#39;</span><span class="c2 c11">)</span></p><p class="c1 c10"><span class="c11 c7 c3 c5">print</span><span class="c2 c11">(hash_table.get(</span><span class="c11 c3 c20">1</span><span class="c2 c11">))</span></p><p class="c1 c10"><span class="c11 c7 c3 c5">print</span><span class="c2 c11">(hash_table.get(</span><span class="c11 c3 c20">5</span><span class="c2 c11">))</span></p><p class="c1 c9 c10"><span class="c2 c11"></span></p><p class="c1"><span class="c3">This is an oversimplified example that clearly has problems. However, the goal is to illustrate how a hash table works. bc</span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.4pa7hqbox18k"><span>Wrapping Up</span></h3><p class="c1 c9"><span></span></p><h3 class="c1 c13" id="h.8iwvho801ojd"><span>Challenge</span><sup><a href="#cmnt54" id="cmnt_ref54">[bb]</a></sup></h3><p class="c1 c9"><span></span></p><h2 class="c1 c13" id="h.evyjmvvmusct"><span>Operating Systems </span></h2><p class="c1"><span class="c3">An operating system is the software that manages a computer&rsquo;s hardware, allowing you to use your computer. Your computer has a limited amount of resources such as memory and CPU, and your operating system determines the resources each program receives, along with creating a structure for managing files, managing different users and managing other common operations needed by programmers. </span></p><h3 class="c1 c13" id="h.r1i62bi3e752"><span>The Kernel</span></h3><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;The kernel is the most fundamental part of an operating system, responsible for allocating resources like CPU and memory to different processes. Processes are programs that are executing. The kernel assigns memory and a stack to each new program when it starts running. The state of the current process is saved in a data structure called a process control block. </span></p><p class="c1"><span class="c3">The kernel cannot be accessed directly, so there is another layer of software built on top of the kernel in order to access it called the shell (because it is a shell around the kernel). We learned how to use the shell in the chapter The Command Line in part III. Operating systems have other responsibilities other than resource sharing, but in order to &nbsp;limit the size of the kernel&rsquo;s code, other operating system jobs (called daemons) are run alongside user programs. </span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.a0e364d8u33u"><span>Scheduling</span><sup><a href="#cmnt55" id="cmnt_ref55">[bc]</a></sup></h3><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;When the kernel switches from one process to another, it is called a context switch. </span></p><p class="c1"><span class="c3">Interupts</span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.kisz64n3gfzt"><span>Threading</span><sup><a href="#cmnt56" id="cmnt_ref56">[bd]</a></sup></h3><p class="c1 c9"><span></span></p><h3 class="c1 c13" id="h.x0lrsf8ew3ep"><span>Concurrency vs. Parallelism </span></h3><p class="c1"><span class="c3">The following explanation on Stack Overflow helped me understand the difference between concurrency and parallelism:</span></p><p class="c1"><span class="c3 c5">&ldquo;Concurrency</span><span class="c3">&nbsp;is when two or more tasks can start, run, and complete in overlapping time periods. It doesn&#39;t necessarily mean they&#39;ll ever both be running at the same instant. Eg. multitasking on a single-core machine.</span></p><p class="c1"><span class="c3 c5">Parallelism</span><span class="c3">&nbsp;is when tasks literally run at the same time, eg. on a multicore processor.&rdquo; RichieHindle 4 .</span></p><p class="c1"><span class="c3">How can a single core processor run multiple tasks at once?</span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.t52aysytsh4l"><span>Wrapping Up</span></h3><p class="c1 c9"><span></span></p><p class="c1 c9"><span></span></p><h3 class="c1 c13" id="h.6dyxmr53vja7"><span>Challenge</span><sup><a href="#cmnt57" id="cmnt_ref57">[be]</a></sup></h3><p class="c1 c9"><span></span></p><h2 class="c1 c13" id="h.jiqehctv5luc"><span>Compilers</span></h2><p class="c1"><span class="c3">&ldquo;Trying to outsmart a compiler defeats much of the purpose of using one.&rdquo;</span></p><p class="c1"><span class="c3">&mdash; Kernighan and Plauger, The Elements of Programming Style.</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c0"><span class="c3">A compiler is a program that takes one format of data and converts it to an format. For example, A C compiler takes C code and converts it to binary so your computer can execute the instructions in the program. Every programming language has a compiler, and the compiler determines the rules of the language. Covering all &nbsp;of Compiler Theory is outside of the scope of this book, but having a basic understanding of compilers will help you be a better programmer. In this chapter, we cover the basics of compilers and build a compiler in Python for our own programming language.</span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.6edn32xnz0lo"><span>Interpreted Vs. Compiled </span></h3><p class="c1"><span class="c35 c3 c5">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">When you are programming in C, you have to compile your program before it will run. The C compiler takes your code and translates it into machine code your computer can understand. Once compiled, you no longer need any sort of program to run your code, you can execute it directly. </span></p><p class="c0"><span class="c3">Python code, however, must always be run using the &ldquo;python&rdquo; program. This is because Python is what is often called an &ldquo;interpreted&rdquo; language, a term used to differentiate it between a language like C, which is called a &ldquo;compiled&rdquo; language. This is confusing, because while Python has an interpreter, it also has a compiler. When you run a Python program, its compiler translates your code to something called bytecode, a special kind of code that is like binary but meant to be consumed by a virtual machine. At runtime, Python&rsquo;s virtual machine translates the bytecode into machine code and executes it line by line. </span></p><p class="c0"><span class="c3">These two approaches both have advantages and disadvantages. One advantage of C&rsquo;s approach is speed&mdash;compiling directly to machine code makes C&rsquo;s programs run faster than Python programs. C&rsquo;s approach also allows for variables to be statically typed. This eliminates a class of errors, but also has drawbacks such as giving programmers less flexibility. Python&rsquo;s approach is advantageous because it allows it to be platform independent. Python&rsquo;s use of an interpreter also allows its variables to be dynamically typed, which makes programming in Python much more flexible than C. </span></p><p class="c0 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.lr1ew1hp5296"><span>Build a Compiler</span></h3><p class="c1"><span class="c11 c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">In this section we are going to build a compiler for a simple Lisp-like language. Lisp is a language invented in 1958 and still used today. &nbsp;The language we are creating is Lisp-like because the languages scope is defined entirely with parenthesis. Our language is going to be able to add, subtract, divide and multiply. We will call our language &ldquo;Mathy&rdquo;. Mathy code will look like this:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(/(+ 4 4) (* 2 2))</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">In our language, the operators are written before the operands. This is called prefix notation, and we are using it because it will make our compiler have to do less work. </span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;The first step in creating our own language is to build a tokenizer, which will take a statement in our language like &ldquo;(/(+ 4 4) (* 2 2))&rdquo; and break it into individual pieces, which is called tokenization. Turning code into tokens is the first phase of a compiler, called a lexer. We want our tokenized output to look like this:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[&#39;(&#39;, &#39;/&#39;, &#39;(&#39;, &#39;+&#39;, &#39;4&#39;, &#39;4&#39;, &#39;)&#39;, &#39;(&#39;, &#39;*&#39;, &#39;2&#39;, &#39;2&#39;, &#39;)&#39;, &#39;)&#39;]</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">We can accomplish this in one line of code &nbsp;using some clever Python:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c7 c3 c5">def </span><span class="c2 c5">tokenize</span><span class="c2">(chars):</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp;</span><span class="c15 c3 c30">&quot;&quot;&quot;</span></p><p class="c1 c10"><span class="c15 c3 c30">&nbsp; &nbsp;</span><span class="c15 c3 c30 c5">:param</span><span class="c15 c3 c30">&nbsp;chars: String of Mathy code.</span></p><p class="c1 c10"><span class="c15 c3 c30">&nbsp; &nbsp;</span><span class="c15 c3 c30 c5">:return</span><span class="c15 c3 c30">: List of tokens.</span></p><p class="c1 c10"><span class="c15 c3 c30">&nbsp; &nbsp;&quot;&quot;&quot;</span></p><p class="c1 c10"><span class="c15 c3 c30">&nbsp; &nbsp;</span><span class="c7 c3 c5">return </span><span class="c2">chars.replace(</span><span class="c40 c3">&#39;(&#39;</span><span class="c7 c3">, </span><span class="c40 c3">&#39; ( &#39;</span><span class="c2">).replace(</span><span class="c40 c3">&#39;)&#39;</span><span class="c7 c3">, </span><span class="c40 c3">&#39; ) &#39;</span><span class="c2">).split()</span></p><p class="c1 c9 c10"><span class="c2"></span></p><p class="c1"><span class="c3">Our function takes &nbsp;a string of Mathy code, and puts a space around all of the opening and closing parenthesis by using the replace function twice. If our code looks like this &ldquo;(2 + 2)&rdquo;, our use of replace will change it to &ldquo;( 2 + 2 )&rdquo;. Now we can use the &ldquo;split&rdquo; function to split our string wherever there is a space (this is the default behavior for split when you don&rsquo;t pass in a parameter). Run our tokenizer to make sure it works:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tokenize(&ldquo;(/(+ 4 4) (* 2 2))&rdquo;)</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; [&#39;(&#39;, &#39;/&#39;, &#39;(&#39;, &#39;+&#39;, &#39;4&#39;, &#39;4&#39;, &#39;)&#39;, &#39;(&#39;, &#39;*&#39;, &#39;2&#39;, &#39;2&#39;, &#39;)&#39;, &#39;)&#39;]</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c0"><span class="c3">The second phase of a compiler is a parser. A parser takes tokens and turns them into a parse tree. </span></p><p class="c0 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.vscat67n1g05"><span>Wrapping Up</span></h3><p class="c1 c9"><span></span></p><h3 class="c1 c13" id="h.leff01dyxqf6"><span>Challenge</span><sup><a href="#cmnt58" id="cmnt_ref58">[bf]</a></sup></h3><p class="c1 c9"><span></span></p><h2 class="c1 c13" id="h.j7bpd5zfuhc6"><span>Object-oriented Programming</span></h2><p class="c1"><span class="c3">&quot;I invented the term Object-Oriented and I can tell you I did not have C++ in mind.&quot;</span></p><p class="c1"><span class="c3">-- Alan Kay</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;A programming paradigm is a style of programming. There are many different programming paradigms, and you won&rsquo;t need to learn all of them at the beginning of your career, but it&rsquo;s important to know what some of the most popular paradigms are. The paradigms we will go over in this chapter are imperative, functional and object-oriented, with a focus on object-oriented programming. &nbsp;</span></p><p class="c1"><span class="c3 c5">State</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;One of the fundamental differences between different programming paradigms is the handling of state. State is the data your program has access to. Programs store data in variables, so state is the value of the variables at a given time in the program&#39;s execution.</span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.vni5wabsaggh"><span>Programming Paradigms</span></h3><p class="c1"><span class="c3">When you first learn to program, you learn how to program imperatively. Imperative programming can be described as &ldquo;do this, then that&rdquo;. An imperative program is a sequence of steps moving toward a solution, with each step changing the program&rsquo;s state. An example of imperative programming would be:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c10"><span class="c3">x = 2</span></p><p class="c1 c10"><span class="c3">y = 4</span></p><p class="c1 c10"><span class="c3">z = 8</span></p><p class="c1 c10"><span class="c3">xyz = x + y + z</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">Each step of the program changes the program&#39;s state. We get to xyz by first defining x, followed by y, followed by z and finally defining xyz.</span></p><p class="c1"><span class="c3">Functional programming is another popular paradigm. It originates from lambda calculus. Functional programming &nbsp;involves creating functions that, given the same input, always return the same result. This is achieved by writing functions that do not change state. &nbsp;</span></p><p class="c1"><span class="c3">Object-oriented programming is a paradigm based around objects passing messages to one another. Objects encapsulate data (state) and methods for altering its state into one place, the object. We will be discussing object-oriented programming for the rest of the chapter. &nbsp;</span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.f3c76ydkpvdv"><span>The Four Pillars of OOP</span></h3><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Inheritance, abstraction, encapsulation and polymorphism are the four main concepts of object-oriented programming. I want to begin this topic by sharing a helpful metaphor I found on Stack Overflow explaining inheritance, encapsulation and polymorphism:</span></p><p class="c1"><span class="c3">&ldquo;Fruit can be eaten, as a general rule, but different types of fruit is eaten in different ways. An apple, which is a fruit, can be eaten (because it is a fruit). A banana can also be eaten (because it is also a fruit), but in a different manner from an apple. You peal it first.Well, at least I do, but I&#39;m weird in some manners so what do I know. This illustrates inheritance (fruit can be eaten), polymorphism (something that eats fruit can eat all types of fruit), and encapsulation (a banana has a skin).&rdquo;</span></p><p class="c1"><span class="c3">We will go over all four concepts in the upcoming sections, as well as revisiting this metaphor. </span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c35 c3 c5">Inheritance </span></p><h3 class="c1 c13" id="h.v11cv3bdvhqp"><span>Composition</span></h3><p class="c1"><span class="c3 c5">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">Composition is another way of extending classes. Compositions means an object stores another object as an attribute. In other words, you are combining two or more objects. You should use composition to represent &ldquo;has a&rdquo; relationships. For example, say we want to represent the relationship between a dog and its owner. This is a &ldquo;has a&rdquo; relationship. A dog has an owner. First we define our dog and people classes:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;class Dog():</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def __init__(self, name, breed, owner):</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.name = name</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.breed = breed</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.owner = owner</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;class Person():</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def __init__(self, name):</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.name = name</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">Now when we create our dog objects, we can pass in a person object:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mick = Person(&ldquo;Mick Jagger&rdquo;)</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Dog(&ldquo;Stanley&rdquo;,&ldquo;French Bulldog&rdquo;, mick)</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">The classic book </span><span class="c15 c3">Design Patterns</span><span class="c3">&nbsp;by Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides, also known as the gang of four, argues you should favor using composition over inheritance when you can, because composition avoids some problems caused by inheritance. For example, the authors explain inheritance breaks encapsulation, writing &ldquo;The implementation of a subclass becomes so bound up with the implementation of its parent class that any change in the parent&rsquo;s implementation will force the subclass to change&rdquo; 13. What this means is that when you start designing with inheritance, you end up having to make a lot of decisions like whether to put functionality or data in the parent class, or the subclass. It can easily get to a point where a change in the parent class can break the subclass. The takeaway here is that composition is a great tool, and you should see if you can use composition to solve a problem before immediately turning to inheritance. </span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.xe51b2b682o2"><span>Polymorphism </span></h3><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Polymorphism, while being a fundamental part of oop, is a concept that can be difficult to understand. It is common to look up the definition of polymorphism and be unable to explain it a few minutes later. I will do my best in this section to explain it well; so that when your interviewer, with an evil look in his eye, asks you to define polymorphism, you knock it out of the park.</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;In the metaphor we went over in the four pillars of OOP, polymorphism is described as the ability to eat a banana because something that eats fruit (people) can eat any fruit. Similarly, </span><span class="c15 c3">Design Patterns</span><span class="c3">&nbsp;explains &ldquo;[Polymorphism] lets a client object make few assumptions about other objects beyond supporting a particular interface.&rdquo;12 But what does this mean and why is it important? </span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Let&rsquo;s start by looking at polymorphism outside the context of object-oriented programming. Take a look at the following code:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print &ldquo;Hello World&rdquo;</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print 200</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print 200.1</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; &ldquo;Hello World&rdquo;</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; 200</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; 200.1</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">This is polymorphism in action. The print function was able to print three data types- a string, an int and a float. We didn&rsquo;t need to call three separate functions - print_string, print_int, print_float in order to print these three data types. </span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Now, think about how you would write a print function in a statically typed language. Remember, in a statically typed language you have to declare what type every parameter is. How would you create a print function that can take any type of object, and print it? Your function cannot accept any type of argument, because you have to declare upon writing the function what type each parameter will be. </span></p><p class="c1"><span class="c3">If you have a function that solves a problem for a string and an int, you want to be able to use the same code for both. You don&rsquo;t want to have to write two separate functions, one for strings and one for ints. This is why polymorphism is important.</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Object-oriented programming makes polymorphism especially easy. Think about the + operator, which is polymorphic. You can use it to add two numbers or two strings:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print &ldquo;Hello &rdquo; + &ldquo;World&rdquo;</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; &ldquo;Hello World&rdquo;</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print 1 + 1</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; 2</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">You can easily create your own class that can be used with the addition operator. All you have to do is override the built in addition method in the class you define:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;class StringNumber:</span></p><p class="c1"><span class="c3">&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def __init__(self, string, number):</span></p><p class="c1"><span class="c3">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;&quot;&quot;</span></p><p class="c1"><span class="c3">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:param string: String to print.</span></p><p class="c1"><span class="c3">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:param number: Number to use when added.</span></p><p class="c1"><span class="c3">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;&quot;&quot;</span></p><p class="c1"><span class="c3">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.string = string</span></p><p class="c1"><span class="c3">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.number = number</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def __repr__(self):</span></p><p class="c1"><span class="c3">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return self.string</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def __add__(self, other):</span></p><p class="c1"><span class="c3">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return self.number + other</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c10"><span class="c3">string_number = StringNumber(&quot;Hello World&quot;, 15)</span></p><p class="c1 c9 c10"><span class="c3"></span></p><p class="c1 c10"><span class="c3">print string_number</span></p><p class="c1 c10"><span class="c3">print string_number + 10</span></p><p class="c1 c9 c10"><span class="c3"></span></p><p class="c1 c10"><span class="c3">&gt;&gt; &ldquo;Hello World&rdquo;</span></p><p class="c1 c10"><span class="c3">&gt;&gt; 25</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">We can create or change any class so the addition operator will know what to do with it. An object being able to consume any other object as long as it adheres to a particular interface is polymorphism. </span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.7y1hedh3xa82"><span>Abstraction</span></h3><p class="c1"><span class="c3 c5">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">We&rsquo;ve talked throughout the book about the idea of a black box, something you use, but do not know how it works. For example, if &nbsp;you wanted to create a Twitter bot, you could use Twitter&rsquo;s API to control it. Twitter provides an API that allows the user to control a Twitter account programmatically, but the user does not know how the Twitter API is actually implemented. That is abstraction. </span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;To understand why abstraction is important, we will take a look at what would happen if we tried to solve a problem without abstraction. Let&rsquo;s say we want to start a business where come up with different stock trades and sell them. Since we don&rsquo;t have much data to begin with, we decide to store all of our data in a list data structure. We write all of the algorithms and code to generate these stock trades, and throughout the code we are using a list to store our data. Since creating algorithms to generate stock picks is complicated, there ends up being over 10,000 lines of code. </span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Eventually, the business becomes so popular, we are generating billion of trade ideas a day. All of the trade ideas won&rsquo;t fit in our list anymore, there isn&rsquo;t enough memory, and we need to start using a database instead. To fix this, we now have to go through 10,000 lines of code, and replace every line that accesses our list data structure with new code that gets data from our database instead. This is clearly time consuming and error prone. We could easily overlook a part of the code that references the old list, which would create errors in our program. </span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Imagine we went through all of that hassle only to realize a few months later we are unhappy with the database we chose to replace the list. We chose MySQL, and a postgreSQL database would be a better fit. Now we have to go back through the exact same process again, changing all of the code once again.</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;We could have solved this problem simply by using abstraction from the beginning. Instead of initially using a list, we should have used a class. The class should have had a method, something like &ldquo;get_data&rdquo;, that returned data. This provides abstraction because the client, (in this case other parts of our code) doesn&rsquo;t care about how we get our data. It could come from a list, a MySQL database, a PostgreSQL database, or any other data source. It doesn&rsquo;t matter. Our get_data method is a black box, our client doesn&rsquo;t need to know, or care what happens inside of it. We can change how our data is created anytime we want, and not have to change a single other line in our 10,000 lines of code. That is the power of abstraction. </span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.b3dfebxn6hmt"><span>Encapsulation</span></h3><p class="c1"><span class="c3 c5">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">While abstraction is powerful, it is only effective as long as there is encapsulation. In our metaphor, encapsulation is the banana peel. With encapsulation, our internal data is hidden. Let&rsquo;s look at our previous example, where we use a class to access data so we can change the implementation of how we get our data whenever we want. Well, if we didn&rsquo;t have encapsulation, even though we are using abstraction, our implementation could be accessed. In our own code, we could choose not to access the implementation, but what if other people are using our code without encapsulation. If we had our get_data method without encapsulation, and decided to store our data in a list at the beginning, a client could choose to ignore using our get_data method, and directly access the list instead. Then, if we changed our code from using a list to a database, our clients code would break. We provided an abstraction for our data, but without encapsulation, we cannot ensure changes to our implementation won&rsquo;t break our client&rsquo;s code. </span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.1tgg331mmwet"><span>MVC</span></h3><p class="c0"><span class="c3">MVC, or model view controller, is a design pattern first introduced in the SmallTalk programming language, by none other than my dad, James Althoff. Hi dad! </span></p><p class="c1"><span class="c3">MVC popularized the idea that an application should be split up into models, views and controllers. A model is something that stores data like a database, a view is the user interface, and a controller is the bridge between the model and the view. By separating these components, you can easily swap each of them out for new ones. </span></p><p class="c1"><span class="c3">For instance, say you create a mobile app using MVC. If you decide to create a website, you could use the original model and controllers, and simply create a new view for the website. The same thing applies if you need to change your models or views. The MVC design pattern promotes good architecture and will help you build better applications. </span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.y39isd8drzrf"><span>Design Patterns</span></h3><p class="c0"><span class="c3">A design pattern is a reusable solution used to solve a specific problem designing software. The seminal book </span><span class="c15 c3">Design Patterns</span><span class="c3">&nbsp;introduced a number of important design patterns to the programming community in 1994, such the Observer pattern &ldquo;which defines a one-to-many relationship between objects such that, when one object changes its state, all dependent objects are notified and updated automatically.&rdquo; 50 You might use this pattern if you were designing a program for users to type a message and have that message to all of their social networks. You could have one class called Message in charge of getting the user&rsquo;s message and use the observer pattern to automatically notify classes called Twitter, Facebook and Tumblr that there is a new message and they should post the message to the social network they represent. </span></p><p class="c0"><span class="c3">&nbsp;Aside from the benefit of solving problems for you, design patterns can help you create better software by making your code easier to understand</span><span class="c3">. If you use a well known design pattern in your program, anyone already familiar with that design pattern will be able to understand your program faster. While a deep dive into design patterns is outside of the scope of this book, you should take some time to familiarize yourself with as many design patterns as you can. </span></p><p class="c0 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.svo5oqkxwj9x"><span>Wrapping Up&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></h3><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Programming paradigms are one of my favorite subjects to study. You can learn just by thinking about what problems each paradigm is trying to solve. In this chapter we focused on object-oriented programming, because in my opinion you can get away with not knowing much about functional programming, but it&rsquo;s much harder to get away without knowing object-oriented programming. One of the reasons is because Java is taught so widely at schools and used at so many companies. Java is an object-oriented language, and doesn&rsquo;t even have functions. However, I do suggest spending some time learning about functional programming. </span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.qxym5wqsxm9l"><span>Challenge</span></h3><p class="c1"><span class="c3">Create the poker game Texas Holdem modeling the entire game with objects.</span></p><p class="c1 c9"><span class="c3"></span></p><h2 class="c1 c13" id="h.9qrrg0plrgmq"><span>Databases</span></h2><p class="c1"><span class="c11 c15 c3 c33 c22">&ldquo;Data! Data! Data! I can&rsquo;t make bricks without clay!&rdquo;</span></p><p class="c1"><span class="c11 c15 c3 c33 c22">-</span><span class="c11 c3 c33 c22">Sir Arthur Conan Doyle</span></p><p class="c1 c9"><span class="c11 c3 c33 c22"></span></p><p class="c1 c9"><span class="c11 c3 c33 c22"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Databases are programs used to persist data. Data persists if it outlives the process that created it 46. Most of the programs we&rsquo;ve built so far work fine without persisting any data, with one notable exception - our web scraping program that collects headlines from Google News we built in part II. All of the headlines we collect are lost after the program stops running. But what if we want to analyze the headlines from the last year? This is where persistence comes in, and why databases are important. </span><span class="c3">Databases perform two main operations- read and write. When you write to a database, you are giving </span><span class="c3">it</span><span class="c3">&nbsp;information to store. When you read from a database, you are retrieving information from it. In an increasingly data centric world, databases are becoming exceedingly important. &nbsp;In this chapter, we go over the basics of databases. </span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.dy5obijdujt7"><span>NoSQL vs. SQL</span></h3><p class="c1"><span class="c3 c5">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">&nbsp;Relational databases were first proposed by Edgar F. Codd in 1970. Relational databases store data like an Excel spreadsheet - data is stored in rows and columns. The data is stored and retrieved using the query language SQL, which we went over &nbsp;in Part III in the chapter of the same name. PostGreSQL and MySQL are examples of popular relational databases. </span></p><p class="c0"><span class="c3">Recently a new breed of databases, called NoSQL &nbsp;have gained popularity. NoSQL literally means &ldquo;no SQL&rdquo;. In other words, the thing these new breed of databases have in common is they are not relational databases using SQL. A few examples of popular NoSQL databases are Redis, MongoDB and Cassandra. Redis is a &ldquo;key value store&rdquo; which means you can store data like a dictionary in Python, ie. store a key and a value. So for example you could store &ldquo;Monty&rdquo; as a key and &ldquo;Python&rdquo; as a value, query Redis for &ldquo;Monty&rdquo; and it will return &ldquo;Python&rdquo;. MongoDB stores data as JSON files- a popular format for storing data, and Cassandra is a column family database. A column family is </span><span class="c3">&ldquo;</span><span class="c3 c48 c22">a NoSQL object that contains </span><span class="c3 c48 c5 c22">columns</span><span class="c3 c48 c22">&nbsp;of related data. It is a tuple (pair) that consists of a key-value pair, where the key is mapped to a value that is a set of </span><span class="c3 c48 c5 c22">columns</span><span class="c3 c48 c22">. In analogy with relational </span><span class="c3 c48 c5 c22">databases</span><span class="c3 c22 c48">, a </span><span class="c3 c48 c5 c22">column family</span><span class="c3 c48 c22">&nbsp;is as a &quot;table&quot;, each key-value pair being a &quot;row&quot;&rdquo; 47</span><span class="c3">&nbsp;Cassandra has its own query language called CQL. Each of these databases have strengths and weaknesses and optimal use cases. The excellent book &ldquo;</span><span class="c3 c22 c57"><a class="c18" href="https://www.google.com/url?q=https://www.goodreads.com/book/show/13610343-nosql-distilled&amp;sa=D&amp;ust=1467337427345000&amp;usg=AFQjCNGthwNxECA3dpq_xIH1nHENRUUG2w">NoSQL Distilled: A Brief Guide to the Emerging World of Polyglot Persistence</a></span><span class="c3">&rdquo; by Pramod Sadalage argues in favor of the concept of polyglot persistence - we shouldn&rsquo;t rely on one database for a project, but rather use multiple databases, using the database that works best to solve the problem at hand. </span></p><p class="c0"><span class="c3">NoSQL databases are especially important in distributed systems. A distributed system is made up of different computers passing messages to each other. Working with distributed systems, you often have multiple databases on different servers that need to communicate. This presents a unique set of challenges, many of which NoSQL databases can help address. While covering distributed systems and NoSQL databases &nbsp;is outside the scope of this book, I encourage you to learn more about both of these subjects. I recommend getting started with the free book &ldquo;Distributed Systems For Fun and Profit&rdquo;:</span><span class="c17 c3"><a class="c18" href="https://www.google.com/url?q=http://book.mixu.net/distsys/single-page.html&amp;sa=D&amp;ust=1467337427346000&amp;usg=AFQjCNEfaA8fQbKfbdnEC6yuEZ8q06Y9Zw">http://book.mixu.net/distsys/single-page.html</a></span><span class="c3">&nbsp;as well as </span><span class="c3">NoSQL Distilled: A Brief Guide to the Emerging World of Polyglot Persistence.</span></p><p class="c0"><span class="c3">. </span></p><p class="c1"><span class="c3">SQL stands for Structured Query Language. It is used to query, or retrieve data, from relational databases. We go into more detail about what a relational database is in the later chapter Databases. Many programming jobs will want you to be able to use SQL. Regardless, SQL plays a large role in software development, and you need to be familiar with it. &nbsp;In this chapter, we will go over the things you will be doing most often with SQL. </span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.4wg9o8f1z36y"><span>Getting Started</span></h3><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;We will get started with SQL by using SQLite. SQLite is a lightweight database that comes built in to your operating system, so we don&rsquo;t have to install anything. Go to the command line and type the command &ldquo;sqlite self_taught.db&rdquo; to create a new database. </span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.39rghkqp7dqn"><span>Coming Back to Your DB</span></h3><p class="c1"><span class="c3">If you are coming back to You can open SQLite anytime with the command &ldquo;sqlite3&rdquo;. If you already created the database, open it with &ldquo;.open self_taught.db&rdquo;. Exit SQLite with the command &ldquo;.exit&rdquo;. </span><sup><a href="#cmnt59" id="cmnt_ref59">[bg]</a></sup></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">#explain relational databases what is a column, what is a row</span><sup><a href="#cmnt60" id="cmnt_ref60">[bh]</a></sup></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.chapqsh6c5rk"><span>Data Types</span><sup><a href="#cmnt61" id="cmnt_ref61">[bi]</a></sup></h3><p class="c1 c9"><span></span></p><h3 class="c1 c13" id="h.yu7be6x51ohm"><span>Create a Table</span></h3><p class="c1 c10"><span class="c11 c3 c16">CREATE TABLE customers</span><span class="c11 c3 c74">(</span></p><p class="c1 c10"><span class="c11 c3 c16">&nbsp; &nbsp; id int<br> &nbsp; &nbsp;first_name</span><span class="c11 c3 c74">&nbsp;text</span><span class="c11 c3 c16"><br> &nbsp; &nbsp;last_name text </span><span class="c11 c3 c74">,</span><span class="c11 c3 c16"><br> &nbsp; &nbsp;date_created date</span><span class="c11 c3 c74">,</span><span class="c11 c3 c16"><br> &nbsp; &nbsp;PRIMARY KEY</span><span class="c11 c3 c74">(</span><span class="c11 c3 c16">column1</span><span class="c11 c3 c74">)</span><span class="c11 c3 c16"><br></span><span class="c11 c3 c74">);</span></p><p class="c1 c9"><span class="c11 c3"></span></p><p class="c1"><span class="c11 c3">Every table in a relational database has to have something called a primary key - a unique identifier for each row. In this case our primary key is our first column - called id. We can set our primary key to auto increment, which means the primary key will be a number that the database automatically increments for you whenever you add new data. Our customers table also has a row for the customer&rsquo;s first name and last name. </span></p><p class="c1"><span class="c3 c5">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">Relational databases are made up of tables that store data like an Excel spreadsheet, in columns and rows. Let&rsquo;s create our first table. Type the following inside sqlite. Make sure you are inside SQLite with the command &ldquo;sqlite3&rdquo; and your command line says sqlite.</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CREATE TABLE self_taught.bdfl(</span></p><p class="c1"><span class="c3">name string</span></p><p class="c1"><span class="c3">project string</span></p><p class="c1"><span class="c3">age int</span></p><p class="c1"><span class="c3">birthday date</span></p><p class="c1"><span class="c3">);</span></p><p class="c1"><span class="c3">Our new in our database self_taught is called &ldquo;bdfl&rdquo;, which stands for benevolent dictator for life. BDFL is a title given to the creators of open source programming projects like Linus Torvalds, creator of Linux, Guido van Rossum creator of Python, David Heinemeier Hansson creator of Ruby on Rails or Matt Mullenweg creator of Wordpress . We will use our table to store data about these bdfls, such as their name, project, and birthdays. </span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.am7a626tn4gf"><span>Constraints</span><sup><a href="#cmnt62" id="cmnt_ref62">[bj]</a></sup></h3><p class="c1"><span class="c11 c3"># find somewhere to put #Creating a relationship with another table is done with SQL , which makes the database is aware of the relationship. You can&rsquo;t just enter any integer under the customer column, your database will only let you enter a valid primary key for a customer in the customer table. </span></p><p class="c1"><span class="c3">A constraint is a rule you can apply to a column that is enforced by the database. Constraints are created using SQL. Here are some examples of constraints you can create:</span></p><p class="c1 c9"><span class="c3"></span></p><ul class="c45 lst-kix_uzlm6n9ijqls-0 start"><li class="c1 c23 c38"><span class="c3">not null - each value in a column must not be NULL</span></li><li class="c1 c23 c38"><span class="c3">unique - value(s) in specified column(s) must be unique for each row in a table</span></li><li class="c1 c23 c38"><span class="c3">primary key - value(s) in specified column(s) must be unique for each row in a table and not be NULL; normally each table in a database should have a primary key - it is used to identify individual records</span></li><li class="c1 c23 c38"><span class="c3">foreign key - value(s) in specified column(s) must reference an existing record in another table (via it&#39;s primary key or some other unique constraint)</span></li><li class="c1 c23 c38"><span class="c3">check - an expression is specified, which must evaluate to true for constraint to be satisfied</span></li></ul><p class="c1"><span class="c3">48</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">If you put the constraint&ldquo;not null&rdquo; on a column, that column cannot be &ldquo;null&rdquo; which is like &ldquo;None&rdquo; in Python. This means the column must have data in it. Say you are collecting the first, middle and last name of subscribers for a newsletter on your website. The table in your database to collect this information might look like this:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;subscribers</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;first | middle | last</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p><p class="c1"><span class="c3">You probably would want to put &ldquo;not null&rdquo; constraints on the first column, while allowing &ldquo;null&rdquo; in the middle and last columns. The reason being that everyone has a first name, but not everyone has a middle or last name. What if Prince signed up for your newsletter? If you put a not null constraint on last, Prince wouldn&rsquo;t be able to sign up. </span></p><p class="c0"><span class="c3">&nbsp;Constraints are important because they make guarantees about data. In this case, if we might want to create a program that analyzes the first names of all of our subscribers, our program would probably perform some sort of operation on each string. If the &ldquo;first&rdquo; column gave our program a null value instead of a string, and our program treated the &nbsp;null value like a string, it would cause an error in our program. By adding a not null constraint to our first column, our program can now be assured that every first name it gets from the database is going to be a string and our program can treat it as such. </span></p><p class="c0"><span class="c3">If we are collecting data for a newsletter, we also need to collect our user&rsquo;s email addresses. We can do this by adding an email column to our table:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3 c22">ALTER TABLE customer</span></p><p class="c0"><span class="c3 c22">ADD email string UNIQUE</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">We added a &ldquo;unique&rdquo; constraint to this new column, so that the email column must be unique. This means if an email is used in one row of our table, it cannot be used in another row. This makes sense because every email address in the world is unique, and if two subscribers have the same email, there is a problem. </span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;A foreign key lets you enforce that the data entered in the table is a primary key in another table. We already saw an example of using a foreign key in our Amazon example. Here is how we could change a table to add a foreign key P_Id to a made up table called Persons:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3 c22">ALTER TABLE customer</span></p><p class="c1 c10"><span class="c3 c22">ADD FOREIGN KEY (P_Id)</span></p><p class="c1 c10"><span class="c3 c22">REFERENCES Persons(P_Id)</span></p><p class="c1 c9"><span class="c3 c22"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;A check constraint is used to make sure data entered in a table meets certain specifications. Here is an example of a check constraint we could add to our email column:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3 c22">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ALTER TABLE customer</span></p><p class="c0"><span class="c3 c22">ADD CHECK email varchar(255)</span></p><p class="c1 c9"><span class="c11 c3 c22"></span></p><p class="c1"><span class="c11 c3 c22">This enforces that all emails entered into our database have to be less than 255 characters. </span></p><p class="c1 c9"><span class="c11 c3 c22"></span></p><h3 class="c1 c13" id="h.afu2wxnagmfi"><span>Insert Data</span></h3><p class="c1"><span class="c3 c5">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">Time to insert data about our first BDFL into our table. Enter the following SQL statements one at a time:</span></p><p class="c0"><span class="c3">INSERT INTO bdfl (name, project,age) </span></p><p class="c0"><span class="c3">VALUES (Guido van Rossum,Python,1-31-1956);</span></p><p class="c1 c23 c9"><span class="c3"></span></p><p class="c0"><span class="c3">INSERT INTO bdfl(name,project,age)</span></p><p class="c0"><span class="c3">VALUES(David Heinemeier Hansson,Ruby on Rails,10-15-1979);</span></p><p class="c1 c23 c9"><span class="c3"></span></p><p class="c0"><span class="c3">INSERT INTO bdfl(name,project,age)</span></p><p class="c0"><span class="c3">VALUES(Linus Torvalds,Linux,12-28-1969);</span></p><p class="c1 c23 c9"><span class="c3"></span></p><p class="c0"><span class="c3">INSERT INTO bdfl(name,project,age)</span></p><p class="c0"><span class="c3">VALUES(Matt Mullenweg,WordPress,1-11-1984);</span></p><p class="c1 c23 c9"><span class="c3"></span></p><p class="c0"><span class="c3">INSERT INTO bdfl(name,project,age)</span></p><p class="c0"><span class="c3">VALUES(Dries Buytaert,Drupal,11-19-1978);</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;INSERT INTO bdfl(name,project,age)</span></p><p class="c0 c10"><span class="c3">VALUES(Larry Wall,Perl,9-27-1954);</span></p><p class="c0 c9 c10"><span class="c3"></span></p><h3 class="c1 c13" id="h.fkjz2vj37mu"><span>Query Data</span></h3><p class="c1"><span class="c3 c5">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">Querying data means looking up data in your database given specific parameters. Example of the parameters you might give are what table the data is in, and different attributes you want the data to contain. Let&rsquo;s start by querying for everything in our bdfl table by entering &ldquo;SELECT* from bdfl&rdquo; into sqlite. &nbsp;In SQL, &ldquo;SELECT *&rdquo; means select everything. </span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;If we do not want to select everything, and just want to select the name of the bdfl of Linux, we can do this with &ldquo;SELECT name FROM bfdfl WHERE project = &ldquo;Linux&rdquo;&rdquo;</span></p><h3 class="c1 c13" id="h.qudgt5eidyev"><span>Or Query</span></h3><p class="c1"><span class="c3 c5">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">You can add &ldquo;or&rdquo; to your query to select from a row if either the condition before the or the condition after the or is true. Select everything from our table where the project is Ruby on Rails or Wordpress with the statement &ldquo;SELECT * FROM bfdl WHERE project = Linux&rdquo; OR</span></p><p class="c1"><span class="c3">&nbsp;project=&rdquo;Ruby on Rails&rdquo;&rdquo;.</span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.tw5l12h7d7ks"><span>And Query</span></h3><p class="c1"><span class="c3 c5">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">Adding &ldquo;and&rdquo; to your query will only select a row if both conditions are true. First try &ldquo;SELECT* FROM bfdl WHERE bfdl.name LIKE D%&rdquo;. This returns any row where the name starts with a D. It should return the data stored for David Heinemeier Hansson and Dries Buytaert. </span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Now try &ldquo;&rdquo;SELECT* FROM bfdl WHERE name LIKE &ldquo;D%&rdquo; AND &nbsp;where A.Date &gt;= Convert(datetime, &#39;2010-04-01&#39; ). This will only return David Heinemeier Hansson. While Dries Buytaert starts with &ldquo;D&rdquo;, his birthday is not greater than the date we provided. </span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.1l4x24pwhykw"><span>Joins</span><sup><a href="#cmnt63" id="cmnt_ref63">[bk]</a></sup></h3><p class="c1 c9"><span></span></p><h3 class="c1 c13" id="h.jmglyz18ck1b"><span>Count</span></h3><p class="c1"><span class="c11 c3">Count the number of rows in a table with:</span></p><p class="c1 c9"><span class="c11 c3"></span></p><p class="c0"><span class="c11 c3">&nbsp;SELECT COUNT(*) FROM bfdl</span></p><p class="c0"><span class="c11 c3">&gt;&gt; </span></p><p class="c0 c9"><span class="c11 c3"></span></p><h3 class="c1 c13" id="h.oa9kar919z2x"><span>Relational Database Design</span></h3><p class="c1"><span class="c11 c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;When you use a relational database, you have to design the different tables your database will have, &nbsp;how the tables relate to each other, what columns they will have, and what constraints are put on those columns. Together, this makes up your database schema. </span><span class="c3">In this section, we are going to to design a schema to store data for a website like Amazon.com.</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;First, we need to think about the data Amazon needs to store. The first thing that comes to mind is products, Amazon clearly must have a database where they store all of their products. Amazon also has to keep track of customers&mdash;you don&rsquo;t have to register a new account every time you order something on Amazon, so they must store their customers information as well. A customer might order more than one product, so Amazon must also have a way to store orders. Let&rsquo;s start by designing a table to hold data about products:</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;product</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ </span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;id | name | price </span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1 &nbsp;| The Pragmatic Programmer | &nbsp;14.99</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p><p class="c1"><span class="c3">Our product table has a column called id that serves as the primary key, and columns for the name of the product and the price. Our primary id is an integer that auto increments, the name column accepts strings, and the price column accepts integers. The data shown in the table such as 1, &ldquo;The Pragmatic Programmer&rdquo; and 14.99 are not part of the database design, but are an example of how data would look in our table. This convention is used throughout this chapter. </span></p><p class="c0"><span class="c3">Note that when you design a database schema, you want to pick a naming convention and stick to it. In this case we will use lowercase letters and an underscore in between words. &nbsp; Now we need a table to keep track of our customers. Our customer table is very similar to our product table:</span></p><p class="c0"><span class="c3">&nbsp;</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3 c16">customer</span></p><p class="c1 c10"><span class="c3 c16">id &nbsp;| first_name | last_name</span></p><p class="c1 c10"><span class="c3 c16">_ _ _ _ _ _ _ _ _ _ _ _ _ _</span></p><p class="c1 c10"><span class="c3 c16">1 | &nbsp; Steve&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp;| Smith</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p><p class="c1"><span class="c3">Our customer table has a primary key, and two columns that accept strings. This is all we need to keep track of our customers. </span></p><p class="c0"><span class="c3">&nbsp;We are going to keep track of our orders using two different tables. The first table will map an order id to a specific customer, and the second table will keep track of the products in each order. Here is our first table shown along with the customer table:</span></p><p class="c0 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3 c16">customer</span></p><p class="c1 c10"><span class="c3 c16">id &nbsp;| first_name | last_name</span></p><p class="c1 c10"><span class="c3 c16">_ _ _ _ _ _ _ _ _ _ _ _ _ _</span></p><p class="c1 c10"><span class="c3 c16">1 | &nbsp; Steve&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp;| Smith</span></p><p class="c1 c9 c10"><span class="c3 c16"></span></p><p class="c1 c10"><span class="c3">order</span></p><p class="c1 c10"><span class="c3">_ _ _ _ _ _ _</span></p><p class="c1 c10"><span class="c3">id | customer</span></p><p class="c1 c10"><span class="c3">1 &nbsp;| 1</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c9 c10"><span class="c3"></span></p><p class="c1"><span class="c3">Our order table has a primary key called id and a column called customer. Our customer column is different than the rest of the columns we&rsquo;ve seen so far because it uses a constraint called a foreign key (covered in the chapter SQL). The customer column of our order table accepts an integer that represents the primary key of a customer in our customer table. In our example, the first entry in the order table customer column is 1. If we look up the row with 1 as its primary key in in our customer table we would get the row &ldquo;</span><span class="c3 c16">1 | &nbsp; Steve | Smith</span><span class="c3">&rdquo;. By using a foreign key, we&rsquo;ve successfully linked our order table to our customer table. This is called creating a relationship. </span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Imagine if we decided to put the information from our &ldquo;customer&rdquo; table in the &ldquo;order&rdquo; table instead:</span></p><p class="c0 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;order</span></p><p class="c0"><span class="c3">id | username | order</span></p><p class="c0"><span class="c3">_ _ _ _ _ _ _ _ _ _ _ _ _</span></p><p class="c0"><span class="c3">1 &nbsp;| Steve &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;| NoSQL Distilled</span></p><p class="c0"><span class="c3">2 &nbsp;| Cory &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | Think Python</span></p><p class="c0"><span class="c3">3 &nbsp;| Steve &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;| The Talent Code</span></p><p class="c0 c9"><span class="c3"></span></p><p class="c1"><span class="c3">The problem with this design is that data is duplicated in our table. The username Steve is repeated twice. If we needed to change Steve&rsquo;s username to &ldquo;Steven&rdquo;, we might accidentally only change the name in the first row, and forget to change it in the third. This would corrupt our data:</span></p><p class="c0"><span class="c3">&nbsp;</span></p><p class="c0"><span class="c3">order</span></p><p class="c0"><span class="c3">id | username | order</span></p><p class="c0"><span class="c3">_ _ _ _ _ _ _ _ _ _ _ _ _</span></p><p class="c0"><span class="c3">1 &nbsp;| Steven &nbsp; &nbsp; | NoSQL Distilled</span></p><p class="c0"><span class="c3">2 &nbsp;| Cory &nbsp; &nbsp; &nbsp; &nbsp; | Think Python</span></p><p class="c0"><span class="c3">3 &nbsp;| Steve &nbsp; &nbsp; &nbsp; &nbsp;| The Talent Code</span></p><p class="c0 c9"><span class="c3"></span></p><p class="c1"><span class="c3">In our original design this is not possible. Take another look at our previous design:</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;customer</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;id | username</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_ _ _ _ _ _ _ _ _</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;8 &nbsp;| Cory</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;9 &nbsp;| Steve </span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;orders</span></p><p class="c0"><span class="c3">id | username | order</span></p><p class="c0"><span class="c3">_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _</span></p><p class="c0"><span class="c3">1 &nbsp;| 9 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;| NoSQL Distilled</span></p><p class="c0"><span class="c3">2 &nbsp;| 8 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;| Think Python</span></p><p class="c0"><span class="c3">3 &nbsp;| 9 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;| The Talent Code</span></p><p class="c0 c9"><span class="c3"></span></p><p class="c1"><span class="c3">When we need to change a username, we only have to change it in one place&mdash;the customer table. Once we change the name in our customer table, anyone looking up username with a foreign key of 9 will see the customer&rsquo;s username is Steve. There is no chance of accidentally corrupting the data because it only exists in one location. </span></p><p class="c0"><span class="c3">Tables can have three types of relationships: one to one, one to many and many to many. This is an example of &nbsp;a oneto one relationship. You create both a one to one relationship and a many to one relationship using a foreign key. The difference is, in a one to one relationship, both tables can have foreign keys to each other, although like in this case, they don&rsquo;t have to. In a one to many relationship, only the many side has a foreign key linking it to the one. This is not something your database knows about, but rather a construct invented to help you design databases.</span></p><p class="c0"><span class="c3">&nbsp;In this example, a customer can have many orders, but an order cannot have many customers. Another example of a many to one relationship is a classroom. A teach can have many classes, but a class cannot have many teachers. In a one to one relationship however, the relationship can go both ways. One person has one passport, and one passport has one person. The final relationship tables can have is called many to many. In order to do that we need to create a junction table, which we need to do in order to complete our Amazon design. Our final table order_item will keep track of products:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c10"><span class="c3">order_item</span></p><p class="c1 c10"><span class="c3">_ _ _ _ _ _ _ _ _ _</span></p><p class="c1 c10"><span class="c3">id | order_id | product_id</span></p><p class="c1 c10"><span class="c3">1 &nbsp;| 1 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;| 1</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">This table has id as a primary key, and two foreign keys- order_id and product_id linking the table to our order and product table. Our design is complete, we can store and lookup all of the information we need to fulfill an order. Here are all of our tables together:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;product</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ </span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;id | name | price </span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1 &nbsp;| The Pragmatic Programmer | &nbsp;14.99</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3 c16">customer</span></p><p class="c1 c10"><span class="c3 c16">id &nbsp;| first_name | last_name</span></p><p class="c1 c10"><span class="c3 c16">_ _ _ _ _ _ _ _ _ _ _ _ _ _</span></p><p class="c1 c10"><span class="c3 c16">1 | &nbsp; Steve&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp;| Smith</span></p><p class="c1 c9 c10"><span class="c3 c16"></span></p><p class="c1 c10"><span class="c3">order</span></p><p class="c1 c10"><span class="c3">_ _ _ _ _ _ _</span></p><p class="c1 c10"><span class="c3">id | customer</span></p><p class="c1 c10"><span class="c3">1 &nbsp;| 1</span></p><p class="c1 c9 c10"><span class="c3"></span></p><p class="c1 c10"><span class="c3">order_item</span></p><p class="c1 c10"><span class="c3">_ _ _ _ _ _ _ _ _ _</span></p><p class="c1 c10"><span class="c3">id | order_id | product_id</span></p><p class="c1 c10"><span class="c3">1 &nbsp;| 1 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;| 1</span></p><p class="c1 c10"><span class="c3">2 &nbsp;| 1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | 1 </span></p><p class="c1 c9 c10"><span class="c3"></span></p><p class="c1"><span class="c3">If we are ready to ship an order, we can get all of the information we need by looking at the order_item table and using that information to query other tables. First we would select all the rows from our order_id with an order_id of 1. Then we would look up all the products using the product_id in each row. Finally, we would use the order_id key to lookup the name of the foreign customer key in our order table, and lookup the customer&#39;s name in the customer table using that information. </span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.etdu767q0pyq"><span>Normalization</span></h3><p class="c0"><span class="c3">One of the challenges you face when working with a database is maintaining data integrity, which means &ldquo;assuring the accuracy and consistency of </span><span class="c3">data</span><span class="c3">&nbsp;over its entire </span><span class="c3">life-cycle</span><span class="c3">&rdquo; 49. Normalization and referential integrity are some of the concepts that help ensure data integrity. </span></p><p class="c0"><span class="c3">Data normalization is the process of designing a relational database in order to reduce data redundancy, which can lead to inaccurate data. While there are many rules for data normalization, there are three specific rules that every database should follow. Each of these rules is called a &ldquo;normal &ldquo;form&rdquo;.</span><span class="c3 c22">&nbsp;If the first rule is followed, the database is in &quot;first normal form&quot; or 1nf. &nbsp;If all three rules are followed, the database is in &quot;third normal form&quot; or 3nf. 52 In order to reach each successive level of normalization, all of the previous rules must be followed. In other words, if the rule for 2nf is satisfied, but 1nf is not, the database is not considered 2nf. </span></p><p class="c0"><span class="c3 c22">To reach the first normal form, you need to avoid duplicating data in multiple row, avoid storing more than one piece of information in a row and the table must have a primary key. Here is an example of storing duplicate data:</span></p><p class="c0 c9"><span class="c3 c22"></span></p><p class="c0"><span class="c3 c22">t-shirt</span></p><p class="c0"><span class="c3 c22">_ _ _ _ _ _ _ _ _ _ _ _ </span></p><p class="c0"><span class="c3 c22">color | color</span></p><p class="c0"><span class="c3 c22">blue&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp;| blue</span></p><p class="c0 c9"><span class="c3 c22"></span></p><p class="c1"><span class="c3 c22">And an example of storing more than one piece of data in one row:</span></p><p class="c1 c9"><span class="c3 c22"></span></p><p class="c1"><span class="c3 c22">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t-shirt</span></p><p class="c1"><span class="c3 c22">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_ _ _ _ _ _ </span></p><p class="c1"><span class="c3 c22">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;color </span></p><p class="c1"><span class="c3 c22">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;blue, large</span></p><p class="c1 c9"><span class="c3 c22"></span></p><p class="c1"><span class="c3 c22">In this example we are using a comma to store two pieces of data in one column&mdash;&rdquo;blue&rdquo; and &ldquo;large&rdquo;. This is something you should never do.Furthermore, neither of these examples are 1nf because they do not have a primary key. </span></p><p class="c1 c9"><span class="c3 c22"></span></p><p class="c1"><span class="c3 c22">Here is an example of a table that is 1nf:</span></p><p class="c1 c9"><span class="c3 c22"></span></p><p class="c1"><span class="c3 c22">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t-shirt</span></p><p class="c1"><span class="c3 c22">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;primary_key = id</span></p><p class="c1"><span class="c3 c22">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_ &nbsp;_ _ _ _ _ _ _ _ _ _ _ _ </span></p><p class="c1"><span class="c3 c22">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;id | color </span></p><p class="c1"><span class="c3 c22">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1 &nbsp; &nbsp; &nbsp;blue&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span></p><p class="c1 c9"><span class="c3 c22"></span></p><p class="c1"><span class="c3 c22">In order for a table to be 2nf, all non primary key columns must relate to the primary key . Let&rsquo;s look at an example that violates 2nf:</span></p><p class="c1 c9"><span class="c3 c22"></span></p><p class="c1"><span class="c3 c22">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t-shirt</span></p><p class="c1"><span class="c3 c22">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;primary_key = item</span></p><p class="c1"><span class="c3 c22">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;primary_key = color</span></p><p class="c1"><span class="c3 c22">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_ _ _ _ _ _ __ _ _ </span></p><p class="c1"><span class="c3 c22">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;item | color |price| tax </span></p><p class="c1"><span class="c3 c22">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t-shirt red &nbsp; &nbsp; &nbsp; 19.99 &nbsp;.90&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p><p class="c1"><span class="c3 c22">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t-shirt blue &nbsp; &nbsp; &nbsp;18.00 .78</span></p><p class="c1"><span class="c3 c22">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;polo &nbsp; &nbsp;yellow &nbsp; 32 &nbsp; &nbsp; &nbsp;1.4</span></p><p class="c1"><span class="c3 c22">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;polo &nbsp; green &nbsp; &nbsp; &nbsp;40 &nbsp; &nbsp; &nbsp;1.8</span></p><p class="c1"><span class="c3 c22">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;polo &nbsp; orange &nbsp; &nbsp; 43 &nbsp; &nbsp; &nbsp;2 </span></p><p class="c1 c9"><span class="c3 c22"></span></p><p class="c1"><span class="c3 c22">This table is not 2nf because the two columns that are not primary keys, price and tax relate to item, but do not relate to color. </span></p><p class="c1 c9"><span class="c3 c22"></span></p><p class="c1 c9"><span class="c3 c22"></span></p><p class="c1"><span class="c3 c22">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dealership</span></p><p class="c1"><span class="c3 c22">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_ _ _ _ _ _ _ _</span></p><p class="c1"><span class="c3 c22">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;id |location &nbsp; | available</span></p><p class="c1"><span class="c3 c22">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1 &nbsp; &nbsp; Portland |Yes</span></p><p class="c1 c9"><span class="c3 c22"></span></p><p class="c1"><span class="c3 c22">53</span></p><p class="c0 c9"><span class="c3 c22"></span></p><p class="c0"><span class="c3 c22">Normalization is an important part of database design. While there even more normalization rules we did not cover, it is important to always normalize your database to 3nf. To help you remember the rules, programmers often &nbsp;use the phrase &ldquo;</span><span class="c3 c22 c79">The data depends on the key [1NF], the whole key [2NF] and nothing but the key [3NF] so help me Codd</span><span class="c3 c22">&rdquo; (Codd, mentioned earlier, is the creator of relational databases) to help them remember the rules of of normalization.</span></p><p class="c1 c9"><span class="c3 c22"></span></p><h3 class="c1 c13" id="h.pr89pksvjlnd"><span>Referential Integrity</span></h3><p class="c0"><span class="c3">Referential integrity is another way of ensuring data integrity. It is a measure of consistency in a database. If for example we have the following tables:</span></p><p class="c0 c9"><span class="c3"></span></p><p class="c0"><span class="c3">customer</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;id | username</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_ _ _ _ _ _ _ _ _</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;8 &nbsp;| Cory</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;9 &nbsp;| Steve</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;order</span></p><p class="c0"><span class="c3">id | username | order</span></p><p class="c0"><span class="c3">_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _</span></p><p class="c0"><span class="c3">1 &nbsp;| 9 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;| NoSQL Distilled</span></p><p class="c0 c9"><span class="c3"></span></p><p class="c1"><span class="c3">and we delete the second row from the customer table:</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p><p class="c0"><span class="c3">customer</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;id | username</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_ _ _ _ _ _ _ _ _</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;8 &nbsp;| Cory</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;order</span></p><p class="c0"><span class="c3">id | username | order</span></p><p class="c0"><span class="c3">_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _</span></p><p class="c0"><span class="c3">1 &nbsp;| 9 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;| NoSQL Distilled</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">Our username column in our order table references a foreign id that no longer exists. This is a violation of referential integrity. Fortunately, your database manages referential integrity for you. If you try to do this in a relational database, it won&rsquo;t let you, you will get an error. </span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.97nl23hat3qb"><span>Indexing </span></h3><p class="c1"><span class="c35 c3 c5">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">You can index a column in a table in order to make reads faster. Indexes work like a telephone book sorted by last name. If you were looking through such a telephone book, you wouldn&rsquo;t look through every single entry, you would immediately skip to the section of the phonebook that matches the last name of the person you were looking for. This is what an index does. When you index a column in a database, internally the database duplicates the data in the column, but arranges it in a specific order - alphabetically for example, that allows it to lookup data faster. Here is an example of creating an index with SQL:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3 c22">CREATE INDEX my_index</span></p><p class="c0"><span class="c3 c22">ON table_name (customers)</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">This will cause our database to internally duplicate all of the data in our customer table and arrange it alphabetically. Now we can lookup customers much faster. The drawback to creating an index is that duplicating data and organizing it has a cost&mdash;it increases the time it takes to write to your database. </span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.pko7t2l129xh"><span>Communicating with Databases </span></h3><p class="c0"><span class="c3">So far, we&rsquo;ve learned to communicate with a database by using SQL from the terminal. But what if we want to store data collected from a program we&rsquo;ve written in a database? In order to achieve this, we need to communicate with our database programmatically, using a</span><span class="c3">&nbsp;database driver, a program that facilitates communication between a programming language and a database. &ldquo;psycopg2&rdquo; is a driver that allows you to interact with PostgreSQL using Python. Here is an example of how we could create and query the Amazon customer table we designed earlier using psycopg2:</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3 c42 c5">import</span><span class="c3 c33">&nbsp;</span><span class="c3 c5 c84">psycopg2</span></p><p class="c1 c9"><span class="c3 c5 c84"></span></p><p class="c1"><span class="c3 c5 c84">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3 c33 c5"># Connect to an existing database<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;conn </span><span class="c3 c50 c5">=</span><span class="c3 c33 c5">&nbsp;psycopg2</span><span class="c3 c50 c5">.</span><span class="c3 c33 c5">connect(</span><span class="c3 c24 c5">&quot;dbname=amazon user=postgres&quot;</span><span class="c3 c33 c5">)</span></p><p class="c1 c9"><span class="c3 c33 c5"></span></p><p class="c0 c89"><span class="c3 c33 c5"># Open a cursor to perform database operations<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cur </span><span class="c3 c50 c5">=</span><span class="c3 c33 c5">&nbsp;conn</span><span class="c3 c50 c5">.</span><span class="c3 c33 c5">cursor()</span></p><p class="c0 c9 c89"><span class="c3 c33 c5"></span></p><p class="c1 c10 c89"><span class="c3 c33 c5"># Execute a command: this creates a new table<br>cur</span><span class="c3 c50 c5">.</span><span class="c3 c33 c5">execute(</span><span class="c3 c24 c5">&quot;CREATE TABLE customer (id serial PRIMARY KEY, username integer);&quot;</span><span class="c3 c33 c5">)</span></p><p class="c0 c9 c89"><span class="c3 c33 c5"></span></p><p class="c1"><span class="c3 c33 c5">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# Pass data to fill a query placeholders and let Psycopg perform<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# the correct conversion (no more SQL injections!)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cur</span><span class="c3 c50 c5">.</span><span class="c3 c33 c5">execute(</span><span class="c3 c24 c5">&quot;INSERT INTO customer (username) VALUES (</span><span class="c15 c3 c5 c87">%s</span><span class="c3 c24 c5">,)&quot;</span><span class="c3 c33 c5">, (</span><span class="c3 c5 c106">&ldquo;cory&rdquo;</span><span class="c3 c33 c5">))<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# Query the database and obtain data as Python objects<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cur</span><span class="c3 c50 c5">.</span><span class="c3 c33 c5">execute(</span><span class="c3 c24 c5">&quot;SELECT * FROM amazon;&quot;</span><span class="c3 c33 c5">)<br></span><span class="c3 c5 c44">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(</span><span class="c3 c33 c5">cur</span><span class="c3 c50 c5">.</span><span class="c3 c33 c5">fetchone())<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; (1, &ldquo;cory&rdquo;)<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# Make the changes to the database persistent<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;conn</span><span class="c3 c50 c5">.</span><span class="c3 c33 c5">commit()<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# Close communication with the database</span></p><p class="c0"><span class="c3 c33 c5">cur</span><span class="c3 c50 c5">.</span><span class="c3 c33 c5">close()<br></span><span class="c3 c44 c5">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3 c33 c5">conn</span><span class="c3 c5 c50">.</span><span class="c3 c33 c5">close()</span></p><p class="c1 c9"><span class="c35 c3 c5"></span></p><p class="c1"><span class="c3">As you can see, we are able to use the psycopg2 module to create our customer table, put data in it, and then retrieve that data (An SQL injection is a type of attack hackers use that we will cover in the next chapter.). </span></p><p class="c0"><span class="c3">While this example allowed us to easily communicate with our database using Python, </span><span class="c3">dealing directly with SQL is burdensome, and there is another tool we can use to abstract SQL away entirely called an ORM, or object relational map. An object relational map is software that lets you interact with your database using classes in your programming language, instead of using SQL. </span></p><p class="c0"><span class="c3">Using an ORM library, such as the ORM that comes with the Python web framework Django, we can represent a database table as a class in Python. Instead of using SQL statements to read and write from our table, we can use methods from the table class we defined. Here is an example of how we could represent and query our Amazon customer table using Django&rsquo;s ORM:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3 c5">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c46 c5">from</span><span class="c78 c77 c5">&nbsp;</span><span class="c92 c77 c5">django.db</span><span class="c78 c77 c5">&nbsp;</span><span class="c46 c5">import</span><span class="c77 c5 c78">&nbsp;models<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c5 c46">class</span><span class="c78 c77 c5">&nbsp;</span><span class="c77 c5 c92">Customer</span><span class="c78 c77 c5">(models</span><span class="c77 c50 c5 c97">.</span><span class="c78 c77 c5">Model):<br> &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp;username </span><span class="c77 c50 c97 c5">=</span><span class="c78 c77 c5">&nbsp;models</span><span class="c77 c50 c97 c5">.</span><span class="c78 c77 c5">CharField()<br> &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; </span></p><p class="c1"><span class="c3">This example defines a new database table called &ldquo;customer&rdquo;. In this case we have two columns, id which is our primary key automatically created by our ORM and not reflected in our code, and username which is a variable set to a CharField object which Django uses to create a database column that stores strings. </span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Now, when we want to interact with our database, we can do it using our Customer class. For example, we can create a new user, which will be stored in MySQL, with the following code:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c0"><span class="c3">user1 = </span><span class="c3">Customer.objects.create</span><span class="c3">(username=&rdquo;eddie&rdquo;)</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">To query our database we simply need to use the class we created:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;eddie = Customer.objects.get(username=&rdquo;eddie&rdquo;)</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print eddie.username</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; &ldquo;eddie&rdquo;</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">With this code, we were able to successfully read and write from our database without any SQL. Django&rsquo;s ORM translates our code to SQL for us, so we don&rsquo;t have to worry about it. </span></p><p class="c0"><span class="c3">You may be wondering how this all works. The key is that our Customer class inherits from a class called Model. Django&rsquo;s ORM uses something called metaclasses that let you run code every time a class is defined. While I don&rsquo;t want to dive too deep into metaclasses, if you are interested in exploring this further, I suggest you check out this excellent post explaining them: </span><span class="c17 c3"><a class="c18" href="https://www.google.com/url?q=http://stackoverflow.com/questions/100003/what-is-a-metaclass-in-python&amp;sa=D&amp;ust=1467337427446000&amp;usg=AFQjCNEgxjrriRJ3hmnHod-HluZOI-XXKA">http://stackoverflow.com/questions/100003/what-is-a-metaclass-in-python</a></span><span class="c3">.</span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.l4oak9u4wyd"><span>Wrapping Up</span><sup><a href="#cmnt64" id="cmnt_ref64">[bl]</a></sup></h3><p class="c1"><span class="c11 c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">Almost any nontrivial application needs to persist data, and the best way to persist data is with a database. There are many different types databases to choose from, including NoSQL and relational databases. Like any technology, there is always a hot new database, the &ldquo;it&rdquo; database of the moment. Instead of investing all of your time learning the newest database fad, you should first spend your time learning relational database fundamentals such as normalization, referential integrity, indexing and how programming languages interact with databases. I know it can be tempting to only focus on the newest technologies, but relational databases aren&rsquo;t going anywhere, and even the author of &ldquo;NoSQL Distilled&rdquo; concedes relational databases should be used in most cases. However, once you have the basics of relational databases mastered, I encourage you to dive into the fascinating world of NoSQL. &nbsp;</span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.f89ut8zg1mif"><span>Challenge</span><sup><a href="#cmnt65" id="cmnt_ref65">[bm]</a></sup></h3><p class="c1 c9"><span></span></p><h2 class="c1 c13" id="h.ofz6411l2obb"><span>Network Programming</span></h2><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;In this chapter, we look into how computers communicate with each other over networks. A network is a group of computers connected through software and hardware that allows them to exchange messages 54. The Internet is an example of a network. In this chapter we will explore the foundation of the internet&mdash; the client server model and the TCP/IP protocol. Then we will dive deeper into these subjects by building both a client and a server. </span></p><h3 class="c1 c13" id="h.8c4tue18974n"><span>Client Server Model</span></h3><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;The Internet communicates using the client server model. In the client-server model, there is a server actively listening for requests (like Google), sent by a client (your web browser). Clients send requests to servers asking for the resources they need to render a webpage, and if everything goes well, the server responds by sending the resources to the browser. Requests are made using HTTP, or hypertext transfer protocol, which we cover in the next section. When I say resources, I mean the HTML, JavaScript and CSS files the browser needs to display a website along with any images. When you visit Google&rsquo;s website, you are seeing the client-server model in action. Google waits for you to send a request</span><span class="c3">, and responds with the resources your web browser needs to display Google&rsquo;s website to you. </span></p><p class="c0"><span class="c3">&nbsp;Try going to Google in your browser and copy and paste the URL into a word processor. You will see there is a slash added to the end of the url. That is because when you go to a website like Google, you are really going to &ldquo;</span><span class="c17 c3"><a class="c18" href="https://www.google.com/url?q=http://www.google.com/&amp;sa=D&amp;ust=1467337427450000&amp;usg=AFQjCNFqKy2vy3A1ogjmkas5r0iXBvaEDg">http://www.google.com/</a></span><span class="c3">&rdquo;. The &ldquo;/&rdquo; is referencing the root page of the website, which you will recall from the Command Line chapter is how is how you reference the root of an operating system. So when you go to &ldquo;</span><span class="c17 c3"><a class="c18" href="https://www.google.com/url?q=http://www.google.com/&amp;sa=D&amp;ust=1467337427450000&amp;usg=AFQjCNFqKy2vy3A1ogjmkas5r0iXBvaEDg">http://www.google.com/</a></span><span class="c3">&rdquo;, you are requesting Googe&rsquo;s root page, whereas if you go to &ldquo;</span><span class="c17 c3"><a class="c18" href="https://www.google.com/url?q=http://www.google.com/news&amp;sa=D&amp;ust=1467337427451000&amp;usg=AFQjCNG2268zdBrJy_IKPqIFds5zhqrrWQ">http://www.google.com/news</a></span><span class="c3">&rdquo; you are requesting &ldquo;/news&rdquo; and Google will respond with different resources. </span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;However, before any of this can happen, your web browser needs to translate &ldquo;</span><span class="c17 c3"><a class="c18" href="https://www.google.com/url?q=http://www.google.com&amp;sa=D&amp;ust=1467337427452000&amp;usg=AFQjCNETzmtskj7tTmlA9_lNyPITNi7IJQ">http://www.google.com</a></span><span class="c3">&rdquo; into an IP address. This is where something called the DNS, or domain name system comes in. The DNS is a giant table that maps all of the domains in the world to their IP addresses, maintained by different internet authorities such as the &nbsp;</span><span class="c3 c22">Internet Assigned Numbers Authority</span><span class="c3">. An ip address is a unique number that represents each computer on the internet. To communicate with Google, your browser needs to get its IP address ,which it does by looking it up in the DNS. </span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;At a low level, all this communication happens through sockets. Sockets are the functions that give programs access to a computer&rsquo;s network hardware. &nbsp;Sockets are created by your operating system as a data structure, allowing computers to establish connections with each other. A server opens a passive socket, and a client opens an active socket. A passive socket stays open indefinitely, listening for connections, whereas an active socket requests data from a passive socket and then closes. </span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;To recap, the client server model works as follows&mdash; a user enters a domain name into the browser, and the browser looks up the domain&rsquo;s IP address in the DNS. The browser sends an http request to the IP address it looked up, and the server responds with an http request letting the browser know it received its request and then sends the resources the web browser needs to display the requested webpage to you. </span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.uwpk3k5m66ai"><span>TCP/IP</span></h3><p class="c1"><span class="c3 c5">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">The communication in the client-server model follows the TCP/IP protocol. </span><span class="c3">A protocol is an agreed upon way of doing things, used to standardize a process. Protocols are not limited to Computer Science. If you were to meet the Queen of England, there would be a protocol in place&mdash;a set of rules every person has to follow when meeting her. You wouldn&rsquo;t just walk up to her and say &ldquo;Hey bro!&rdquo;. You would address her a certain way, speak politely, stick to certain subjects etc. That is a protocol. </span></p><p class="c0"><span class="c3">Computers communicating over the Internet use a protocol called TCP/IP. Imagine an internet without an agreed upon protocol. With no standard for communicating, every time two computer&rsquo;s needed to pass data to one another, they would have to negotiate the terms of their communication.Nothing would ever get done. &nbsp;Luckily we have protocols like TCP/IP that ensures communication happens seamlessly.</span></p><p class="c0"><span class="c3">TCP/IP is what is called a protocol stack. It is is made up of four layers, with each layer using its own protocol. Each layer is a program responsible for accomplishing a task, and communicating with the layers above and below it. &nbsp;While the Internet could use one protocol (instead of a stack), the benefit of using a protocol stack separated into layers is that you can make changes to one layer without needing to change the others. Think about the post office. Someone at the post office accepts packages, then someone else sorts them and passes them off to someone who delivers them. Each person has their own protocol for accomplishing their task (and they all communicate with each other). If the delivery guy decides to deliver packages using drones instead of a truck, the change in protocol doesn&rsquo;t affect the person that accepts packages or the person that sorts them. This is the same reason why TCP/IP uses a protocol stack, so changes to one protocol won&rsquo;t affect the others.</span></p><p class="c0"><span class="c3">&nbsp;The four layers of TCP/IP are the Application Layer, the Transport Layer, the Internet Layer and the Network Layer. &nbsp;</span></p><p class="c0 c9"><span class="c3"></span></p><p class="c0"><span class="c3">[tcp/ip picture]</span></p><p class="c0 c9"><span class="c3"></span></p><p class="c1"><span class="c3">Let&rsquo;s take a look at an example of data moving through TCP/IP by once again thinking about mail. </span><span class="c3">Think of the Application Layer as a program containing a letter. When you type a url into your web browser, the Application Layer writes a message on the letter that looks something like this:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[picture of letter with http on it]</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">The information on the letter is an HTTP request. HTTP is a protocol that servers and clients use to send messages to each other. It contains information such as the requested resource, the browser the client is using and a few more pieces of information. </span></p><p class="c0"><span class="c3">The letter is then passed to the next layer, the Transport Layer. You can think of the Transport Layer putting the letter in an envelope. Outside the envelope, the Transport Layer puts more information:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[picture of envelope with writing on it]</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">The information includes the domain name to send the request to, the domain the request is coming from, the port number the server is on, and something called a checksum. Data is not sent across the network all at once, it is broken up into packets which are sent one at a time. The Transport Layer keeps uses checksum to make sure all the packets get delivered properly. </span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Now the Transport Layer passes the envelop to the Internet Layer which takes the envelope and puts it in an even bigger envelop, with more information written on it:</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[picture of envelop with writing on it]</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p><p class="c1"><span class="c3">The information written on the Internet Layer envelop only contains the information the router needs to deliver the data to the server it is sending the data to. It contains the IP address of the server and the IP address of the computer making the request. It also contains the TTL, which stands for time to live and (explain ttl) At this point, the envelop is considered a packet. </span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;This final envelope is sent to the bottom layer, the Network Layer, which uses hardware and software to physically send the data. The data is received by the Network Layer on the servers computer and the envelop is passed in reverse order up the protocol stack with each layer removing an envelop until the letter is revealed at the Application Level of the server. The server then goes through the same process through the TCP/IP stack, sending an HTTP request back signalling that the request was either valid or invalid. If the request was valid, it starts sending the resources the client needs. </span></p><p class="c0"><span class="c3">It is important to remember that data does not get sent all at once, it gets broken down into packets. The bottom layer of the stack, the Network Layer may send thousands of packets to a client, &nbsp;</span></p><p class="c0 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.kgle1c7wjkza"><span>Create a Server</span></h3><p class="c0"><span class="c3">In this section we are going to use the Python socket library to create a simple web server and client, using Python&rsquo;s built-in library for creating and managing sockets. We are going to create a server that listens for requests and responds to them with the date and a client we can use to make those requests.</span></p><p class="c0"><span class="c3">A web server creates a socket, binds it to a port, and then runs an infinite loop, responding to requests as they come through the socket; whereas a client simply opens up a socket, connects to a server to get the information it needs. We will start by building a &nbsp;server. &nbsp;The first step is to import the socket and date libraries:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c0"><span class="c3">import socket</span></p><p class="c0"><span class="c3">import datetime</span></p><p class="c0 c9"><span class="c3"></span></p><p class="c1"><span class="c3">First we get today&rsquo;s date:</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;today = str(datetime.datetime.today())</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">Now we can create a socket:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c0"><span class="c3">s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">socket.AF_INET is an address family specifying what addresses in your socket can communicate with. AF_INET is used for communicating over the internet. There are other address families like AF_BLUETOOTH that can be passed in for communicating over Bluetooth. &ldquo;socket.SOCK_STREAM&rdquo; means we want to use TCP to ensure delivery. #fact check</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">Next we bind our socket to TCP port 8888:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s.bind(&ldquo;&rdquo;, 8888)</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">And set the length of the queue (a queue is used because multiple requests can come in at the same time and a data structure is needed to process them):</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s.listen(10)</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">Now we can create the servers infinite loop which waits for a connection and sends the date back as a response:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c10"><span class="c3">while</span><span class="c3 c5">&nbsp;</span><span class="c3">True:</span></p><p class="c1 c10"><span class="c3">&nbsp; &nbsp; connect, address = s.accept()</span></p><p class="c1 c10"><span class="c3">&nbsp; &nbsp; resp = (connect.recv(1024)).strip() &nbsp; # limit request to 1024 bytes</span></p><p class="c0"><span class="c3">&nbsp; &nbsp;connect.send(&ldquo;received http request&rdquo;)</span></p><p class="c0"><span class="c3">&nbsp; &nbsp;#Close the connection when we are finished:</span></p><p class="c0"><span class="c3">&nbsp; connect.close()</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">Here is our full server:</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p><p class="c1 c10"><span class="c3">import</span><span class="c3 c5">&nbsp;</span><span class="c3">socket</span></p><p class="c1 c10"><span class="c3">import datetime</span></p><p class="c1 c9 c10"><span class="c3"></span></p><p class="c1 c10"><span class="c3">today = datetime.datetime.today()</span></p><p class="c1 c10"><span class="c3">s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)</span></p><p class="c1 c10"><span class="c3">s.bind((&quot;&quot;, 8888))</span></p><p class="c1 c10"><span class="c3">s.listen(5)</span></p><p class="c1 c9 c10"><span class="c3"></span></p><p class="c1 c10"><span class="c3">while True:</span></p><p class="c1 c10"><span class="c3">&nbsp; &nbsp; &nbsp; &nbsp;connect, address = s.accept()</span></p><p class="c1 c10"><span class="c3">&nbsp; &nbsp; &nbsp; &nbsp;resp = (connect.recv(1024)).strip()</span></p><p class="c1 c10"><span class="c3">&nbsp; &nbsp; &nbsp; &nbsp;connect.send(today)</span></p><p class="c1 c10"><span class="c3">&nbsp; &nbsp; &nbsp; &nbsp;connect.close()</span></p><p class="c1 c10"><span class="c3">&nbsp; &nbsp; &nbsp;</span></p><p class="c1 c9 c10"><span class="c3"></span></p><p class="c1"><span class="c3">You can test this server by running the program and going to localhost:8888 in your browser. You should see the date when you do. </span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.6m2tpn2xgw7i"><span>Create a Client</span></h3><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Now lets &nbsp;create a client to make requests to our server. Just like creating a web server, we start out by creating a socket:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;import socket</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">To connect to our server, we get the name of our local machine and set the variable port to the port our server uses so we can use it later:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# get name of our local machine</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;host = socket.gethostname() &nbsp; &nbsp; &nbsp;</span></p><p class="c0"><span class="c3"># set port &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;port = 8888</span></p><p class="c1 c9"><span class="c3 c5"></span></p><p class="c1"><span class="c3">Now we can connect to our hostname at port 8888 by passing in a tuple with our hostname and port:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s.connect((hostname, port))</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">Save the response and close the socket:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg = s.recv()</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s.close()</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">Print the message we received:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(&ldquo;{}&rdquo;.format(msg))</span></p><p class="c1"><span>That&rsquo;s all there is to it. We&rsquo;ve built a functioning client. When you run our client, you will get the date from our server. </span></p><p class="c1 c9"><span></span></p><h3 class="c1 c13" id="h.a46i61dj3b31"><span>Wrapping Up</span></h3><p class="c0"><span class="c3">There is a lot more to network programming than we were able to cover in this chapter, but this should give you a good foundation to build on. &nbsp;</span><span class="c3 c81">Hopefully, building your own server and client has improved your understanding of how the internet works. </span><span class="c3">Whether you want to build websites or apps, you almost certainly be dealing with networks at some point, &nbsp;and I encourage you to continue learning about networks. </span></p><p class="c1 c9"><span></span></p><h3 class="c1 c13" id="h.bx8j1t8a8rxv"><span>Challenge</span><sup><a href="#cmnt66" id="cmnt_ref66">[bn]</a></sup></h3><p class="c1 c9"><span class="c3"></span></p><h2 class="c1 c13" id="h.b8x0sigv63ht"><span>Security</span></h2><p class="c1"><span class="c3">&ldquo;Security is a state of mind.&rdquo;</span></p><p class="c1"><span class="c3">&mdash; NSA Security Manual</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Security is an easy subject for the self-taught programmer to ignore. You probably won&rsquo;t be asked about security in your interview, and security is not important for the throw away programs you write while you are learning to program. However, once you get your first job programming, you are directly responsible for the security of the code you write. The last thing you want is for your company&#39;s database to get hacked because you made a mistake. Better to learn some practical tips to keep your code safe now. </span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.l1z6wy9z8nvz"><span>Don&rsquo;t Log in As Root</span></h3><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;In the chapter The Command Line we went over the use of Sudo. You use sudo to issue a command as the root user, without having to log in as the root user. This is common knowledge among experienced programmers, and advice I follow, but I didn&rsquo;t know why I shouldn&rsquo;t log in as the root user. Here is a great explination I found from Darth Android on superuser.com:</span></p><p class="c1 c9"><span class="c3"></span></p><ul class="c45 lst-kix_k99j9p95k9dh-0 start"><li class="c1 c10 c38"><span class="c3">Every hacker / virus knows that there is a root account. If they are blindly attacking a system, it&#39;s a known entry point, and very likely to be a target. This is why your root account should have logins disabled.</span></li><li class="c1 c10 c38"><span class="c3">Everyone makes mistakes. You&#39;ll be in a rush, accidentally hit the wrong key, not double-check/realize the full list of files in that wildcard you just entered... eventually, you&#39;ll be sitting at your system thinking. &quot;Crap. I did not want to do that. How do I undo that?&quot; It happens to the best of us. By not using the root account, you can be relatively confident that whatever you just did, did </span><span class="c15 c3">not</span><span class="c3">&nbsp;mess up your system&#39;s ability to operate.</span></li><li class="c1 c10 c38"><span class="c3">Privilege escalation - If there is a security vulnerability that&#39;s exploited (in say, your web browser), by not running your programs as root will limit damage. If your web browser is running as root (because you logged in as root), then any security failures will have access to your entire system.</span></li><li class="c1 c10 c38"><span class="c3">Acountability - There is only one root account. If everything is using the root account, it&#39;s difficult to find out who did what. This applies less in a single-user environment, but that&#39;s still not a good argument to avoid good security practices. With something like sudo, every command that&#39;s executed with super-user powers is logged, along with the specific user that requested it be executed.</span></li></ul><p class="c1"><span class="c3">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 7.</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">The verdict is clear. While it may be tempting, do not login as root! </span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.qitudi67xmk3"><span>Never Trust User Input</span></h3><p class="c0"><span class="c3">There are several kinds of malicious attacks that rely on exploiting programs that accept user input. A</span><span class="c3">n OS injection is an example of one of these attacks. An OS injection can occur when take user input and feed it into a command on your operating system. In his blog post, Dangerous Python Functions, Part 1, Kevin London gives a great example of a situation where this could happen. In the following example, we are trying to &ldquo;transcode a user-specified video file into a different format&rdquo;18.</span><span class="c3 c5">&nbsp;</span><span class="c3">We do this by asking the user to provide the path for the file to transcode using Python&rsquo;s built-in raw_input function. </span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#WARNING do not run this program!</span></p><p class="c1 c10"><span class="c3">import subprocess</span></p><p class="c1 c9 c10"><span class="c3"></span></p><p class="c1 c10"><span class="c3 c5">def</span><span class="c3">&nbsp;</span><span class="c3 c5">transcode_file</span><span class="c3">():</span></p><p class="c1 c10"><span class="c3">&nbsp; &nbsp; filename </span><span class="c3 c5">=</span><span class="c3">&nbsp;raw_input(&#39;Please provide the path for the file to transcode: &#39;)</span></p><p class="c1 c10"><span class="c3">&nbsp; &nbsp; command </span><span class="c3 c5">=</span><span class="c3">&nbsp;&#39;ffmpeg -i &quot;{source}&quot; output_file.mpg&#39;</span><span class="c3 c5">.</span><span class="c3">format(source</span><span class="c3 c5">=</span><span class="c3">filename)</span></p><p class="c1 c10"><span class="c3">&nbsp; &nbsp; subprocess</span><span class="c3 c5">.</span><span class="c3">call(command, shell</span><span class="c3 c5">=</span><span class="c3">True) &nbsp;</span><span class="c15 c3"># a bad idea!</span></p><p class="c1 c9"><span class="c15 c3"></span></p><p class="c1"><span class="c3">The problem with this code is it is vulnerable to an OS injection. A user could type the following text when prompted by raw_input: </span></p><p class="c1 c9"><span class="c3"></span></p><p class="c0"><span class="c3">&ldquo;; rm -rf /&rdquo;. </span></p><p class="c0 c9"><span class="c3"></span></p><p class="c1"><span class="c3">The variable &ldquo;command&rdquo;: would then be &nbsp;&ldquo;ffmpeg -i; rm -rf/ output_file.mpg&rdquo;:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;command = &ldquo;ffmpeg -i; rm -rf/ output_file.mpg&rdquo;</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">Next, &ldquo;subprocess</span><span class="c3 c5">.</span><span class="c3">call(command, shell</span><span class="c3 c5">=</span><span class="c3">True) &rdquo;, used to execute operating system commands using Python ,would execute this command, deleting all of the files on the computer running the program. The important thing to remember is that if you are writing commands in the terminal, you can write two commands on the same line by ending the first command with a semicolon. So for example, if we wanted to print &ldquo;hello&rdquo; in the terminal twice, we could do it with &ldquo;echo hello;echo hello;&rdquo;. The malicious user is taking advantage of this by using a semicolon to terminate the first command, and then adding in his own command, which will also execute. In this case the user added the command &ldquo;rm -rf/ output_file.mpg&rdquo; which will delete all the files on the system. </span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3 c5">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">&nbsp;SQL injections are another type of attack similar to OS injection but on databases. An SQL injection occurs when a user submits input with SQL in it, allowing them to execute SQL in your database.For instance, you might have a car website, &nbsp;and on that website you want to return details about a specific car a user enters. You expect the user to provide you with the name of a car, such as &ldquo;Nissan Leaf&rdquo;. &nbsp;Once you receive the name of the car from the user, you write more code to query a MySQL table called cars to display details about the car. You use the following query:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SELECT* FROM car WHERE name = query_data;</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">The variable query_data represents the data you collected from the user. If the user enters something like &ldquo;Nissan Leaf&rdquo; like you expected, this works fine. The problem is the user can enter anything they want. A malicious user could enter something like this into the website:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&ldquo;Ferrari&rdquo;; DROP TABLE cars;</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">This would cause your program to query MYSQL with the following, valid SQL:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SELECT* FROM car WHERE name = &ldquo;Ferrari&rdquo;; DROP TABLE cars</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">The query would get all of the data for a car named Ferrari, then it would delete your database table wiping out all of your data. </span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;XSS, or cross-site scripting is another type of attack relying on unsanitized user input, specifically not sanitizing HTML. Say you go to a website that allows users to answer questions posted by other users. The site allows anyone to post a response, and in that response they can post HTML, like a link to another website. If the website doesn&rsquo;t sanitize the HTML, a user could upload something like this to their post:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c10"><span class="c3">&lt;A HREF=</span><span class="c17 c3"><a class="c18" href="https://www.google.com/url?q=http://www.mybank.com/login.cgi?clientprofile%3D&amp;sa=D&amp;ust=1467337427498000&amp;usg=AFQjCNGy_w4iQfvKhZcXiflK8DDvsdiPYw">http://www.someothersite.com/userprofile.cgi?clientprofile=</a></span><span class="c3">&lt;SCRIPT&gt;[malicious script loaded from another website]&lt;/SCRIPT&gt;&gt;Check out this awesome link &lt;/A&gt; </span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">You might look at this link and think it is just a link to another site, with a script to figure out the client profile to go to, when really the script is full of malicious code trying to exploit vulnerabilities in your web browser. </span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;The theme of all these attacks are that they come from user input. When your program accepts user input, you should always assume the input is malicious.</span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.qzh2okbi74q0"><span>Minimize Your Attack Surface</span></h3><p class="c1"><span class="c3 c5 c35">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">Another strategy for keeping your software secure is to minimize your attack surface. Your attack surface is made up of the different areas of your program where attackers could extract data or attack your system in some way. By making this area as small as possible, you can reduce the likelihood of vulnerabilities in your program. Here are some strategies for minimizing your attack surface:</span></p><p class="c1"><span class="c3">&nbsp; </span></p><ul class="c45 lst-kix_7lx1v6khmzxb-0 start"><li class="c32 c1 c10"><span class="c3 c22">Don&rsquo;t give more access to users that you absolutely have to. Limit users to the least possible level of access required to accomplish their tasks.</span></li><li class="c32 c1 c10"><span class="c3 c22">Store as little confidential data as possible. Quickly isolate or destroy sensitive data when it is no longer needed. Do not continue to collect sensitive data unless you have a use for it.</span></li><li class="c32 c1 c10"><span class="c3 c22">Turn off functionality that is not required or being used. Get rid of features that are not being used. Remove previous versions of software and unused code &mdash; a major source of software insecurity.</span></li><li class="c32 c1 c10"><span class="c3 c22">Reduce entry points into your application: eliminate unnecessary service requests. This may include email services, database access, DLLs, web pages, mobile applications, and other API calls.</span></li><li class="c32 c1 c10"><span class="c3 c22">Reduce reliance on third-party APIs and interfaces: give careful consideration to any add-ons that originate outside your organization.</span></li><li class="c32 c1 c10"><span class="c3 c22">Before upgrading or de-bugging your application, identify risks to your existing system: What is changing? What will be done differently after the changes? What new vulnerabilities could be exposed?</span></li><li class="c1 c10 c32"><span class="c3 c22">Zero Access by Default. Scale user security </span><span class="c15 c3 c22">upward</span><span class="c3 c22">&nbsp;instead of </span><span class="c15 c3 c22">downward</span><span class="c3 c22">: start at &lsquo;zero&rsquo; and permit access to functions and data as required, rather than starting with an &lsquo;all-access pass&rsquo; and turning off bits and pieces to reduce privilege levels.</span></li></ul><p class="c1 c99"><span class="c3 c22">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;52</span></p><p class="c1"><span class="c3">To summarize, try to limit the amount of third party code you use, restrict users as much as possible, don&rsquo;t store sensitive data you don&rsquo;t need, and remove code /services that aren&rsquo;t being used. Following these suggestions will help keep your software secure. </span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.g1l63mpxx1b2"><span>Wrapping Up</span></h3><p class="c0"><span class="c3">Avoiding logging in as the root user on your system, not trusting user input and minimizing your attack surface are important steps to making sure your programs are secure. However, like the quote at the beginning of this chapter says, &ldquo;Security is a state of mind&rdquo;. You should always take time to try to think like a hacker. How would a hacker try to exploit your code? This can help you find vulnerabilities you otherwise would&rsquo;ve overlooked. There is a lot more to learn about security than we can cover in this book, so make an effort to always be thinking and learning about security.</span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.vvm89e4elre8"><span>Challenge</span><sup><a href="#cmnt67" id="cmnt_ref67">[bo]</a></sup></h3><h2 class="c1 c13" id="h.jgqe3p9izeig"><span>Let&rsquo;s Read Some Code</span></h2><h2 class="c1 c13" id="h.40cezcimkyp3"><span>Bringing It All Together</span></h2><p class="c1 c9"><span class="c3"></span></p><h2 class="c1 c13" id="h.23dgjxqviczf"><span>Practice</span></h2><h3 class="c1 c13" id="h.23dgjxqviczf-2"><span>Exercises</span></h3><h3 class="c1 c13" id="h.23dgjxqviczf-3"><span>Read</span><sup><a href="#cmnt68" id="cmnt_ref68">[bp]</a></sup></h3><ol class="c45 lst-kix_dsyvh3rdzgxo-0 start" start="1"><li class="c1 c9 c10 c38"><span></span></li><li class="c1 c10 c38"><span class="c17 c3"><a class="c18" href="https://www.google.com/url?q=http://stackoverflow.com/questions/2794016/what-should-every-programmer-know-about-security&amp;sa=D&amp;ust=1467337427507000&amp;usg=AFQjCNF036aZbSZpPVrFiHmU96_6CggMAg">http://stackoverflow.com/questions/2794016/what-should-every-programmer-know-about-security</a></span></li></ol><p class="c1 c9"><span class="c3"></span></p><p class="c1 c9"><span class="c3"></span></p><h1 class="c1 c13" id="h.e77hbc4l6ore"><span>Part V Learn Best Practices</span></h1><h2 class="c1 c13" id="h.6goz48hyiage"><span>Testing </span></h2><p class="c1"><span class="c3">&ldquo;If debugging is the process of removing bugs, then programming must be the process of putting them in.&rdquo; </span></p><p class="c1"><span class="c3">-Edsger Dijkstra</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Testing a program means checking to make sure everything works as expected. For example, if you write a function, you want to test to make sure passing in different types of data will not break it, among other things. You can do this manually (although this is not a good idea), or preferably, by writing code to test your programs automatically. A good programmer does not consider testing optional, therefore you should consider every program you write incomplete until you have written tests for it. As someone smart once said, &ldquo;Untested code is broken code&rdquo;.</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;In this chapter, we are going to go over some of the fundamentals of testing. Fun fact&mdash;the word computer &ldquo;bug&rdquo; originates from an incident in 1942, where Grace Murray Hopper found and removed a moth stuck in a Mark Aiken Relay Calculator. </span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.sapah5m97x7t"><span>Testing Saves Time</span></h3><p class="c0"><span class="c3">It&rsquo;s easy to get lazy and skip testing, justifying it by saying you don&rsquo;t have time to write tests. Counterintuitively, taking the time to write tests will save you a substantial amount of time in the long run. The reason is because if you don&rsquo;t write tests, you will end up testing your software manually&mdash; running your program with various different inputs to see if anything breaks. There are a few problems with this&mdash;the first is its unreliable. You will probably forget to test all of the inputs you need to every time you test manually. Spending your time manually testing inputs for something you could easily automate is also a huge waste of time. Finally, if you come back to the project in a month, you won&rsquo;t be able to remember the different tests you were manually running.</span></p><h3 class="c1 c13" id="h.ut5i8p70m27z"><span>Unit Tests</span></h3><p class="c0"><span class="c3">Unit tests &nbsp;test individual pieces of code. You should write unit tests for all of your functions and classes. You should try to break your code with your unit tests. Test different parameter types. Try calling your classes in ways you didn&rsquo;t think about initially. Try to get into the head of whoever is going to be using your code. Think about how they are going to be using it, and if there is anything they might try to do you might not expect. </span></p><h3 class="c1 c13" id="h.4ka7jtghhssq"><span>Assertions</span></h3><p class="c1"><span class="c3 c5">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">You use the </span><span class="c4">assert</span><span class="c3">&nbsp;keyword to check whether or not a condition is true, and raise an </span><span class="c4">AssertionError</span><span class="c3">&nbsp;if it&rsquo;s not.</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x = 1</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;assert x == 1</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt;</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x = 1</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;assert x == 2</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt;Traceback (most recent call last):</span></p><p class="c1"><span class="c3">&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;File &quot;/Users/coryalthoff/PycharmProjects/self_taught/st.py&quot;, line 2, in &lt;module&gt;</span></p><p class="c1"><span class="c3">&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;assert x == 1</span></p><p class="c0"><span class="c3">AssertionError</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">The program will run without any exceptions. Assertions test for a condition and raise an exception when they are false. In the first example, the condition tested by our assertion is false, so it raises and error. In the second example, the condition is true, so no error is raised. </span></p><h3 class="c1 c13" id="h.tum102n5bj45"><span>Unit Testing Framework</span></h3><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;You should write your unit tests within a unit test framework. A unit test framework will come with code for creating and structuring unit tests. Python has a built in unit test framework called unittest and Rspec is a popular unit test framework for Ruby. </span></p><h3 class="c1 c13" id="h.gej7w7g7k8b0"><span>Example</span></h3><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;The unit testing framework you choose should come with various assert methods. Here are a few examples of &nbsp;unit tests from the Python docs:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c10"><span class="c3">import unittest</span></p><p class="c1 c9 c10"><span class="c3"></span></p><p class="c1 c10"><span class="c3">class TestStringMethods(unittest.TestCase):</span></p><p class="c1 c9 c10"><span class="c3"></span></p><p class="c1 c10"><span class="c3">&nbsp; def test_upper(self):</span></p><p class="c1 c10"><span class="c3">&nbsp; &nbsp; &nbsp; self.assertEqual(&#39;foo&#39;.upper(), &#39;FOO&#39;)</span></p><p class="c1 c9 c10"><span class="c3"></span></p><p class="c1 c10"><span class="c3">&nbsp; def test_isupper(self):</span></p><p class="c1 c10"><span class="c3">&nbsp; &nbsp; &nbsp; self.assertTrue(&#39;FOO&#39;.isupper())</span></p><p class="c1 c10"><span class="c3">&nbsp; &nbsp; &nbsp; self.assertFalse(&#39;Foo&#39;.isupper())</span></p><p class="c1 c9 c10"><span class="c3"></span></p><p class="c1 c10"><span class="c3">&nbsp; def test_split(self):</span></p><p class="c1 c10"><span class="c3">&nbsp; &nbsp; &nbsp; s = &#39;hello world&#39;</span></p><p class="c1 c10"><span class="c3">&nbsp; &nbsp; &nbsp; self.assertEqual(s.split(), [&#39;hello&#39;, &#39;world&#39;])</span></p><p class="c1 c10"><span class="c3">&nbsp; &nbsp; &nbsp; </span><span class="c15 c3"># check that s.split fails when the separator is not a string</span></p><p class="c1 c10"><span class="c3">&nbsp; &nbsp; &nbsp; with self.assertRaises(TypeError):</span></p><p class="c1 c10"><span class="c3">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; s.split(2)</span></p><p class="c1 c9 c10"><span class="c3"></span></p><p class="c1 c10"><span class="c3">if __name__ == &#39;__main__&#39;:</span></p><p class="c1 c10"><span class="c3">&nbsp; &nbsp; unittest.main()</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">This is similar to the previous example, but it introduces some of the assertions that come with Python&rsquo;s built in unittest module- assertEqual, assertTrue, assertFalse and assertRaises. These assertions are pretty self explanatory, assertEqual takes two arguments and checks if they are equal, assertTrue checks if a condition is true, and assertFalse checks if a condition is false. &ldquo;assertRaises&rdquo; checks to make sure a condition raises an exception. If you use assertRaises and the condition doesn&rsquo;t raise an exception, assertRaises will raise an exception!</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Another thing to keep in mind, each method you define within your &nbsp;unit test framework is an isolated test. In the Python unittest framework, you have to start your methods with &ldquo;test&rdquo;.</span></p><h3 class="c1 c13" id="h.oh2vbncgjoac"><span>Setup &amp; Teardown</span></h3><p class="c1"><span class="c3 c5">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">Your unit test framework should come with a built in method for setting up the conditions of your tests, and a method for cleaning up after each test. Take a look at the following example from the Python docs:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c10"><span class="c3">import unittest</span></p><p class="c1 c9 c10"><span class="c3"></span></p><p class="c1 c10"><span class="c3">class WidgetTestCase(unittest.TestCase):</span></p><p class="c1 c10"><span class="c3">&nbsp; &nbsp; def setUp(self):</span></p><p class="c1 c10"><span class="c3">&nbsp; &nbsp; &nbsp; &nbsp; self.widget = Widget(&#39;The widget&#39;)</span></p><p class="c1 c9 c10"><span class="c3"></span></p><p class="c1 c10"><span class="c3">&nbsp; &nbsp; def tearDown(self):</span></p><p class="c1 c10"><span class="c3">&nbsp; &nbsp; &nbsp; &nbsp; self.widget.dispose()</span></p><p class="c1 c10"><span class="c3">&nbsp; &nbsp; &nbsp; &nbsp; self.widget = None</span></p><p class="c1 c9 c10"><span class="c3"></span></p><p class="c1 c10"><span class="c3">&nbsp; &nbsp; def test_default_size(self):</span></p><p class="c1 c10"><span class="c3">&nbsp; &nbsp; &nbsp; &nbsp; self.assertEqual(self.widget.size(), (50,50),</span></p><p class="c1 c10"><span class="c3">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&#39;incorrect default size&#39;)</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">The method setUp runs before each individual test. In this example it creates a new widget object. After every test runs, the method tearDown is called. In this example it calls dispose on the widget which presumably does some sort of cleanup, and then sets self.widget to None. </span></p><h3 class="c1 c13" id="h.3c9jw0ho09p"><span>Regression Tests</span></h3><p class="c1"><span class="c3 c5">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">Regression tests compare a functions new output with a previous output. For example, say you are writing a function that creates a text file with data in it. You could save the file your function creates, and write a test that checks the output of your function against your saved file. That way, your test will alert you if any changes you make cause your function to output incorrect data. &nbsp; &nbsp;</span></p><h3 class="c1 c13" id="h.bx5tik9bufzj"><span>TDD</span></h3><p class="c1"><span class="c3 c5">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">TDD stands for test driven development. When you follow test driven development, it means you write your tests before you write any code. I recommend following TDD because it forces you to break out of the pattern of putting tests off until the end of your development cycle, and then deciding not to write them. It also guarantees you will have tests throughout your development cycle, so you will never be tempted to test something manually &ldquo;just this one time&rdquo;. </span></p><p class="c1"><span class="c3 c5">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p><h3 class="c1 c13" id="h.vc47d8fuk4o5"><span>Wrapping Up</span></h3><p class="c1 c9"><span></span></p><h3 class="c1 c13" id="h.5q5gzslsjg53"><span>Challenge</span></h3><p class="c1"><span class="c3">Pick out a web framework, and writing tests for the following code:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">def add(x, y):</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return x + y </span></p><p class="c1 c9"><span></span></p><h2 class="c1 c13" id="h.jazegstwlbin"><span>Logging</span></h2><p class="c1"><span class="c3">&nbsp;&ldquo;One of the differences between a great programmer and a bad programmer is that a great programmer adds logging and tools that make it easy to debug the program when things fail.&rdquo;</span></p><p class="c1"><span class="c3">- Henrik Warne</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Programmers use logging to help debug and to gain additional insight into what what happened when a program ran. </span></p><h3 class="c1 c13" id="h.a6gjeyv809c3"><span>Logging Exceptions</span></h3><p class="c1"><span class="c3">It is common practice to write code to catch exceptions. For example, in Python &nbsp;you might write a function like this:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def divide(a, b):</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;try:</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; print a / b</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;except &nbsp;ZeroDivisionError:</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; Logger.log(&ldquo;{}/{} cannot divide by zero&rdquo;).format(a,b)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">In this function we are printing the result of a/b. However, if the user inputs b as 0, print a/b is going to throw an exception since you cannot divide by zero. The except part of the code catches the error. If we call our divide function with divide(1,0), the line of code &ldquo;Logger.log(&ldquo;{}/{} cannot divide by zero&rdquo;).format(a,b)&rdquo; will print out a message to our console saying &ldquo;1/0 cannot divide by zero&rdquo;.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;When something goes wrong in your program, you don&rsquo;t want it to go unnoticed. You should log information about what happened so you can review it later. &nbsp;</span></p><h3 class="c1 c13" id="h.l16dglvkw35u"><span>Collecting Data</span></h3><p class="c1"><span class="c3 c5">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">Logging is also useful for collecting and analyzing data. For example, you may have a web server set to log data, including the date and time, every time it receives a request. You could store all of your logs in a database, and create another program to analyze that data and create a graph displaying the most popular times of day for your website. 8</span></p><h3 class="c1 c13" id="h.i9bz4psqrgm3"><span>Logging Levels</span></h3><p class="c1"><span class="c3 c5">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">You may be wondering why you should use a logger, when you can easily print something to the console using a print statement. One reason is that with a logger, you can use levels. When you log something to the console, you can give it a rating in terms of how severe it is. For example, you can log something as &ldquo;info&rdquo; or you can log it as &ldquo;critical&rdquo;. Something logged as info is generally something that will not effect the program, but another developer might be interested in seeing. Something logged as critical might mean something catastrophic has happened and the program is in serious danger. </span></p><h3 class="c1 c13" id="h.umsj41xtbqij"><span>Wrapping Up</span></h3><p class="c1 c9"><span></span></p><h3 class="c1 c13" id="h.4yw7hkvt3hll"><span>Challenge</span><sup><a href="#cmnt69" id="cmnt_ref69">[bq]</a></sup></h3><h2 class="c1 c13" id="h.9c07fn2a38ph"><span>Good Programming Practices</span></h2><p class="c1"><span class="c3">&ldquo;Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.&rdquo; </span></p><p class="c1"><span class="c3">-Martin Golding</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">Learning a few general programming principles will help you write better code. Many of these principles originated in the excellent book The Pragmatic Programmer by Andy Hunt and Dave Thomas. The Pragmatic Programmer was a huge influence on me, and reading it dramatically improved the quality of my code. I highly recommend you read this book at some point in your programming career. </span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.2xng54kbfwid"><span>Write Code As A Last Resort &nbsp;</span></h3><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Your job as a software engineer is to write as little code as possible. When you have a problem you need to solve, your first thought should not be how can I solve this? It should be, has someone else solved this problem already? If you are trying to solve a common problem, chances are someone else has solved it. Start by looking online for a solution. Only after you&rsquo;ve determined no one else has already solved the problem should you start solving it yourself. </span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.6mpvlnkhtofi"><span class="c3 c82">DRY</span></h3><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;One important principle is called DRY or Don&rsquo;t Repeat Yourself. If you are writing code and you find yourself repeating yourself, stop. Do not repeat yourself. If you find that you are copying pieces of code, pasting them somewhere else, and making small changes to create new code, you are repeating yourself. &nbsp;Say for example, you are writing a program that can make changes to a list of words:</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p><p class="c1 c10"><span class="c7 c3 c5">def </span><span class="c2 c5">capitalize_item</span><span class="c2">(word</span><span class="c7 c3">, </span><span class="c2">word_list):</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp;</span><span class="c15 c3 c30">&quot;&quot;&quot;</span></p><p class="c1 c10"><span class="c15 c3 c30">&nbsp; &nbsp;</span><span class="c15 c3 c30 c5">:param</span><span class="c15 c3 c30">&nbsp;word: String word to change in list.</span></p><p class="c1 c10"><span class="c15 c3 c30">&nbsp; &nbsp;</span><span class="c15 c3 c30 c5">:param</span><span class="c15 c3 c30">&nbsp;word_list: List of string words.</span></p><p class="c1 c10"><span class="c15 c3 c30">&nbsp; &nbsp;&quot;&quot;&quot;</span></p><p class="c1 c10"><span class="c15 c3 c30">&nbsp; &nbsp;</span><span class="c7 c3 c5">for </span><span class="c2">index</span><span class="c7 c3">, </span><span class="c2">item </span><span class="c7 c3 c5">in </span><span class="c3 c25">enumerate</span><span class="c2">(word_list):</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c7 c3 c5">if </span><span class="c2">item == word:</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;word_list[index] = word_list[index].capitalize()</span></p><p class="c1 c9"><span class="c2"></span></p><p class="c1 c10"><span class="c7 c3 c5">def </span><span class="c2 c5">change_letter</span><span class="c2">(word</span><span class="c7 c3">, </span><span class="c2">word_list</span><span class="c7 c3">, </span><span class="c2">old_letter</span><span class="c7 c3">, </span><span class="c2">new_letter):</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp;</span><span class="c15 c3 c30">&quot;&quot;&quot;</span></p><p class="c1 c10"><span class="c15 c3 c30">&nbsp; &nbsp;</span><span class="c15 c3 c30 c5">:param</span><span class="c15 c3 c30">&nbsp;word: String word to change in list.</span></p><p class="c1 c10"><span class="c15 c3 c30">&nbsp; &nbsp;</span><span class="c15 c3 c30 c5">:param</span><span class="c15 c3 c30">&nbsp;word_list: List of string words.</span></p><p class="c1 c10"><span class="c15 c3 c30">&nbsp; &nbsp;</span><span class="c15 c3 c30 c5">:param</span><span class="c15 c3 c30">&nbsp;old_letter: String letter to change in word.</span></p><p class="c1 c10"><span class="c15 c3 c30">&nbsp; &nbsp;</span><span class="c15 c3 c30 c5">:param</span><span class="c15 c3 c30">&nbsp;new_letter: String letter to change old_letter to in word.</span></p><p class="c1 c10"><span class="c15 c3 c30">&nbsp; &nbsp;&quot;&quot;&quot;</span></p><p class="c1 c10"><span class="c15 c3 c30">&nbsp; &nbsp;</span><span class="c7 c3 c5">for </span><span class="c2">index</span><span class="c7 c3">, </span><span class="c2">item </span><span class="c7 c3 c5">in </span><span class="c3 c25">enumerate</span><span class="c2">(word_list):</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c7 c3 c5">if </span><span class="c2">item == word:</span></p><p class="c1 c10"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;word_list[index] = word_list[index].replace(old_letter</span><span class="c7 c3">, </span><span class="c2">new_letter)</span></p><p class="c1 c9 c10"><span class="c3"></span></p><p class="c1 c10"><span class="c2">words = [</span><span class="c40 c3">&#39;Programming&#39;</span><span class="c7 c3">, </span><span class="c40 c3">&#39;is&#39;</span><span class="c7 c3">, </span><span class="c40 c3">&#39;fun&#39;</span><span class="c2">]</span></p><p class="c1 c10"><span class="c2">upper(</span><span class="c40 c3">&#39;Programming&#39;</span><span class="c7 c3">, </span><span class="c2">words)</span></p><p class="c1 c10"><span class="c7 c3 c5">print</span><span class="c2">(words)</span></p><p class="c1 c10"><span class="c2">change_letter(</span><span class="c40 c3">&#39;fun&#39;</span><span class="c7 c3">, </span><span class="c2">words</span><span class="c7 c3">, </span><span class="c40 c3">&#39;u&#39;</span><span class="c7 c3">, </span><span class="c40 c3">&#39;$&#39;</span><span class="c2">)</span></p><p class="c1 c10"><span class="c7 c3 c5">print</span><span class="c2">(words)</span></p><p class="c1 c9 c10"><span class="c2"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; [&#39;PROGRAMMING&#39;, &#39;is&#39;, &#39;fun&#39;]</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; [&#39;PROGRAMMING&#39;, &#39;is&#39;, &#39;f$n&#39;]</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">Our program works, but if you look closely at the code, you will see both of our functions look for a word in a word list to replace it with something new. Instead of duplicating the code to search for a word in both functions, we should create one function that returns the index of the word we are looking for. (note that Python has a built in function index() that finds the index of a string in a list but for the sake of this example we are not using it). Here is how we should refactor our code to avoid repeating ourselves:</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c7 c3 c5">def </span><span class="c2 c5">find_index</span><span class="c2">(word</span><span class="c7 c3">, </span><span class="c2">word_list):</span></p><p class="c1 c23"><span class="c2">&nbsp; &nbsp;</span><span class="c15 c3 c30">&quot;&quot;&quot;</span></p><p class="c1 c23"><span class="c15 c3 c30">&nbsp; &nbsp;</span><span class="c15 c3 c30 c5">:param</span><span class="c15 c3 c30">&nbsp;word:</span></p><p class="c1 c23"><span class="c15 c3 c30">&nbsp; &nbsp;</span><span class="c15 c3 c30 c5">:param</span><span class="c15 c3 c30">&nbsp;word_list:</span></p><p class="c1 c23"><span class="c15 c3 c30">&nbsp; &nbsp;</span><span class="c15 c3 c30 c5">:return</span><span class="c15 c3 c30">:</span></p><p class="c1 c23"><span class="c15 c3 c30">&nbsp; &nbsp;&quot;&quot;&quot;</span></p><p class="c1 c23"><span class="c15 c3 c30">&nbsp; &nbsp;</span><span class="c7 c3 c5">for </span><span class="c2">index</span><span class="c7 c3">, </span><span class="c2">item </span><span class="c7 c3 c5">in </span><span class="c3 c25">enumerate</span><span class="c2">(word_list):</span></p><p class="c1 c23"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c7 c3 c5">if </span><span class="c2">item == word:</span></p><p class="c1 c23"><span class="c2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span><span class="c7 c3 c5">return </span><span class="c2">index</span></p><p class="c1 c23 c9"><span class="c2"></span></p><p class="c1 c23 c9"><span class="c2"></span></p><p class="c1 c23"><span class="c7 c3 c5">def </span><span class="c2 c5">upper</span><span class="c2">(word</span><span class="c7 c3">, </span><span class="c2">word_list):</span></p><p class="c1 c23"><span class="c2">&nbsp; &nbsp;</span><span class="c15 c3 c30">&quot;&quot;&quot;</span></p><p class="c1 c23"><span class="c15 c3 c30">&nbsp; &nbsp;</span><span class="c15 c3 c30 c5">:param</span><span class="c15 c3 c30">&nbsp;word: String word to change in list.</span></p><p class="c1 c23"><span class="c15 c3 c30">&nbsp; &nbsp;</span><span class="c15 c3 c30 c5">:param</span><span class="c15 c3 c30">&nbsp;word_list: List of string words.</span></p><p class="c1 c23"><span class="c15 c3 c30">&nbsp; &nbsp;&quot;&quot;&quot;</span></p><p class="c1 c23"><span class="c15 c3 c30">&nbsp; &nbsp;</span><span class="c2">index = find_index(word</span><span class="c7 c3">, </span><span class="c2">word_list)</span></p><p class="c1 c23"><span class="c2">&nbsp; &nbsp;word_list[index] = word_list[index].upper()</span></p><p class="c1 c23 c9"><span class="c2"></span></p><p class="c1 c23 c9"><span class="c2"></span></p><p class="c1 c23"><span class="c7 c3 c5">def </span><span class="c2 c5">change_letter</span><span class="c2">(word</span><span class="c7 c3">, </span><span class="c2">word_list</span><span class="c7 c3">, </span><span class="c2">old_letter</span><span class="c7 c3">, </span><span class="c2">new_letter):</span></p><p class="c1 c23"><span class="c2">&nbsp; &nbsp;</span><span class="c15 c3 c30">&quot;&quot;&quot;</span></p><p class="c1 c23"><span class="c15 c3 c30">&nbsp; &nbsp;</span><span class="c15 c3 c30 c5">:param</span><span class="c15 c3 c30">&nbsp;word: String word to change in list.</span></p><p class="c1 c23"><span class="c15 c3 c30">&nbsp; &nbsp;</span><span class="c15 c3 c30 c5">:param</span><span class="c15 c3 c30">&nbsp;word_list: List of string words.</span></p><p class="c1 c23"><span class="c15 c3 c30">&nbsp; &nbsp;</span><span class="c15 c3 c30 c5">:param</span><span class="c15 c3 c30">&nbsp;old_letter: String letter to change in word.</span></p><p class="c1 c23"><span class="c15 c3 c30">&nbsp; &nbsp;</span><span class="c15 c3 c30 c5">:param</span><span class="c15 c3 c30">&nbsp;new_letter: String letter to change old_letter to in word.</span></p><p class="c1 c23"><span class="c15 c3 c30">&nbsp; &nbsp;&quot;&quot;&quot;</span></p><p class="c1 c23"><span class="c15 c3 c30">&nbsp; &nbsp;</span><span class="c2">index = find_index(word</span><span class="c7 c3">, </span><span class="c2">word_list)</span></p><p class="c1 c23"><span class="c2">&nbsp; &nbsp;word_list[index] = word_list[index].replace(old_letter</span><span class="c7 c3">, </span><span class="c2">new_letter)</span></p><p class="c1 c9"><span class="c11 c7 c3 c5"></span></p><p class="c1"><span class="c3">By creating a new function that returns the index of a word, we are no longer repeating code. If we decide to change the way we search for an index, we only need to change our find_index function, instead of changing code in multiple functions. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p><h3 class="c1 c13" id="h.jxenhfn942w8"><span>Orthogonality </span></h3><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Orthogonality is another important principle popularized by the book the </span><span class="c15 c3">Pragmatic Programmer</span><span class="c3">. The author explains &ldquo;In computing, the term has come to signify a kind of independence or decoupling. Two or more things are orthogonal if changes in one do not affect any of the others. In a well-designed system, the database code will be orthogonal to the user interface: you can change the interface without affecting the database, and swap databases without changing the interface.&rdquo;16. The author follows up with a perfect example of a non orthogonal system- a helicopter. </span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.mn2qvjudah8h"><span>Every Piece Of Data Should Have One Representation</span></h3><p class="c1"><span class="c3">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;This is best explained with an example. Say you are building a project and in that project you are using Twitter&rsquo;s API. In order to use Twitter&rsquo;s API, you registered for an API key. You are using the API key in two functions. The first function gets data from celebrities, and the second function gets data from non celebrities. In the first function, you define a variable called api_key and assign it to your api key. In your second function you do the same thing. Both functions now have access to the api key. </span></p><p class="c1"><span class="c3">A few months go by and you end up getting a new API key from Twitter. You go to the celebrity function and change the variable api_key to the new api_key. It&rsquo;s been a long time, and you completely forget the Twitter API key is even used in the second function. You put your code into production;accidently leaving the old API key in the second function, everything breaks, and you get fired. You could&#39;ve avoided all of this by following the rule that every piece of data should have one representation. The correct way to handle this situation is to make a configuration file. Both functions should get the API key from the configuration file. That way, no matter how many places the API key is used, if it changes, it only needs to be changed in one place. </span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.aoeo0rhwhbqk"><span>Functions Should Do One Thing</span></h3><p class="c1"><span class="c3">Every function you write should do one thing. If you find your functions getting too long, ask yourself if the function you are writing is doing more than one thing. This offers several advantages. First of all, your code will be easier to read because the name of your function will describe exactly what it does. If something in your code isn&rsquo;t working, it will be easier to debug if every function is responsible for a specific task because you can simply look at what is not working and isolate that function. As Ryan Singer says, &ldquo;So much complexity in software comes from trying to make one thing do two things.&rdquo; </span></p><h3 class="c1 c13" id="h.nw4vwp4kejkv"><span>Docstrings</span></h3><p class="c1"><span class="c3 c5">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">The Python community has influenced my view on docstrings, which is to say I consider them essential, especially in a dynamic language. Docstrings are comments at the top of a function or method that explain what it does and documents what types the parameters should be. Here is an example:</span></p><p class="c1"><span class="c3">def add(x, y):</span></p><p class="c1"><span class="c3">&nbsp; &nbsp; &ldquo;&rdquo;&rdquo;Adds two integers together.</span></p><p class="c1"><span class="c3">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; :param int x first integer to be added.</span></p><p class="c1"><span class="c3">&nbsp; &nbsp; &nbsp; &nbsp;:param int y second integer to be added&ldquo;&rdquo;&rdquo;.</span></p><p class="c1"><span class="c3">&nbsp; &nbsp; &nbsp; &nbsp;:return int sum of x + y.</span></p><p class="c1"><span class="c3">&nbsp; &nbsp; &nbsp; &nbsp;return x + y&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p><p class="c1"><span class="c3">There are two important things happening here. The first is in the first line of the docstring. It clearly explains what the function does. You might be thinking this is not important because you could just look at the code. Do not make this mistake. When other developers reuse your code, they do not want to have to read through your code to figure out what you are doing. If you write a complicated function, after a few months even you will forget what it does. </span></p><p class="c1"><span class="c3">The second important thing to notice about the docstring is the parameters and return value of the function are documented. As I said before, developers do not want to have to read through your code to figure out what it does. They want to read a summary of what it does, what they need to give the function and what it will return.</span></p><p class="c1"><span class="c3">&nbsp;This is even more important t in a dynamic language because the parameters are not self documenting. In a statically typed language, you are required to give your parameters a type. Here is the same example in the statically typed language Golang.</span></p><p class="c1"><span class="c3">func add(x int, y int) int {</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return x + y</span></p><p class="c1"><span class="c3">}</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;As you can see, in the Python example, if I removed the docstring, it would be impossible to tell by looking at x and y what type they should be. If I want to use the function add, I don&rsquo;t know whether to pass in an int or a float. In the Golang example, however, it is perfectly clear what type the parameters should be - they should be ints. &nbsp;</span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.7v5smg52hth1"><span>Use Dummy Data</span></h3><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;While I was at eBay I was given an assignment to fix an error in some code. What made this difficult was the program processed a large text file and took five minutes to run. My debugging cycle looking a little like this- make a change, run the program and wait five minutes for the results. Needless to say, I was not making much progress. I finally took the time to substitute the large text file with &ldquo;dummy data&rdquo;, fake data my program could use, so I could still text it, but would only take a few seconds to process. Although it is annoying to take the time to set up dummy data, even if it takes you twenty minutes, you will quickly make that time up plus much more by shortening your debug cycle. &nbsp;Changing things your program uses at runtime is called monkey patching. </span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.43vf8ayqp2b8"><span>If It&rsquo;s Taking Too Long You Are Probably Making a Mistake</span></h3><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;If you are not working on something obviously complex like doing a mapreduce job on a billion rows of data, and your program is taking minutes to load, assume you are doing something wrong.</span></p><p class="c1"><span class="c3">&nbsp;</span></p><h3 class="c1 c13" id="h.eqyjrlfze1sv"><span>Do Things The Best Way The First Time</span></h3><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;If you are in a situation where you are doing something and you think, I know there is a better way of doing this, but I&rsquo;m in the middle of coding and don&rsquo;t want to stop and figure out how to do it better. Stop. Do it better. </span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.bxhvb9txkik2"><span>Follow Conventions</span></h3><p class="c1"><span class="c3">Furthermore, programming languages themselves also have their own conventions. These include agreed upon rules like how to name functions. Taking the time to learn the conventions of a new programming language you are trying to learn will help you read code written in the new language faster.</span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.6oxbttzfaki0"><span>Code Reviews</span></h3><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Code Reviews are when someone other than you reads your code and gives you feedback. You need to do code reviews. It is especially important as a self-taught programmer getting started. Even if you follow all of the best practices laid out in this chapter, you are still doing things wrong. You need someone more experienced than you to read over your code and tell you what you are doing wrong so you can fix it. </span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.eney32hjes5"><span>Pep-8</span></h3><p class="c1"><span class="c3 c5">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">Pep-8 is a set of guidelines for writing Python code. For example, Pep-8 specifies classes in Python should always be uppercase. You can read Pep-8 here: </span><span class="c17 c3"><a class="c18" href="https://www.google.com/url?q=https://www.python.org/dev/peps/pep-0008&amp;sa=D&amp;ust=1467337427583000&amp;usg=AFQjCNEB5q4axoT_qGMoWyAdBCdfDuqcSw">https://www.python.org/dev/peps/pep-0008</a></span><span class="c3">.</span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.426d3t89vvm"><span>Separate Logic</span></h3><p class="c1"><span class="c11 c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Business logic should be separate from database logic. </span></p><p class="c1 c9"><span class="c3"></span></p><h3 class="c1 c13" id="h.xjmiqmvvxffu"><span>Challenge &nbsp;</span></h3><p class="c1"><span class="c3">Write at least 100 lines of code following the good programming practices listed in this chapter as carefully as possible and post your code on codereview.stackechange.com</span></p><h2 class="c1 c13" id="h.vdiplircjll5"><span>Let&rsquo;s Read Some Code</span><sup><a href="#cmnt70" id="cmnt_ref70">[br]</a></sup></h2><h2 class="c1 c13" id="h.alfxjz2tc8tp"><span>Bringing It All Together</span><sup><a href="#cmnt71" id="cmnt_ref71">[bs]</a></sup></h2><h2 class="c1 c13" id="h.hil10kqqkydp"><span>Practice</span><sup><a href="#cmnt72" id="cmnt_ref72">[bt]</a></sup></h2><h1 class="c1 c13" id="h.go01qf1u1nwk"><span>Part VI Land a Job</span></h1><h2 class="c1 c13" id="h.fic869m5mrbk"><span>Your First Programming Job</span></h2><p class="c1"><span class="c3">&ldquo;Beware of &ldquo;the real world&rdquo;. A speaker&rsquo;s appeal to it is always an invitation not to challenge his tacit assumptions.&rdquo;</span></p><p class="c1"><span class="c3">&mdash; Edsger W. Dijkstra</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3 c5">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">Once you&rsquo;ve mastered the concepts in this book, you will be ready to get your first job as a software engineer. Getting your first programming job is of course more difficult than getting subsequent jobs, however if you follow the advice in this chapter, you should have no problem. Luckily, you land your first programming job and get some experience, when it comes time to look for your next job; not only will it be easy to get an interview, but recruiters will start reaching out to you. </span></p><h3 class="c1 c13" id="h.yj6z9xu8sihc"><span>Getting Initial Experience</span></h3><p class="c1"><span class="c3 c5">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">In order to get your first programming job you need some experience. But how do you get programming &nbsp;experience if no one will hire you without it? There are a few ways to solve this problem. One solution is to focus on open source. Open source software is software developed by programmers spread across the world, available for anyone to use for free. You can either start your own open source project, or contribute to the thousands of open source projects on Github. </span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Another option is to do freelance work. You can create a profile on sites like Upwork, and start getting small programming jobs right away. To get started, I recommend finding someone you know that needs some programming work done. Have them sign up for an Upwork account and officially hire you on Upwork. That way they can leave you a good review for your work. Until you have at least one good review on a site like Upwork, it is difficult to land jobs. Once people see that you&rsquo;ve successfully completed at least one job, it gets much easier. &nbsp;In the article What every computer science major should know, Matt Might explains students should focus on building excellent portfolios instead of focusing on their resumes, a sentiment I agree with. I recommend taking a look at the portfolios highlighted in the article:</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c17 c3"><a class="c18" href="https://www.google.com/url?q=http://ezyang.com/&amp;sa=D&amp;ust=1467337427589000&amp;usg=AFQjCNFKqA7vWhSHr8no-jdyIKZljlUO1A">http://ezyang.com/</a></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c17 c3"><a class="c18" href="https://www.google.com/url?q=http://www.mjbshaw.com/&amp;sa=D&amp;ust=1467337427589000&amp;usg=AFQjCNGjQO58YrMGTitRBReLedP6BaS7fg">http://www.mjbshaw.com/</a></span></p><h3 class="c1 c13" id="h.fk6kkjkx8rcj"><span>Choose a Path</span></h3><p class="c0"><span class="c3">One thing to keep in mind is that programming jobs are lumped into specific domains, each with their own set of technologies. If you look at programming job ads, they will often read something like &ldquo;Backend Ruby Programmer Wanted&rdquo;. This means they are looking for someone that programs the backend of a website, and is ideally already familiar with Ruby. If you go to the job description, there will be a list of things the ideal candidate will be familiar with. While it&rsquo;s fine to be a generalist while you are learning to program, and it is possible to get a job as a &ldquo;generalist&rdquo; programmer, &nbsp;you probably should find a specific area of programming you enjoy and start becoming an expert in it. This will make getting a job significantly easier. </span></p><p class="c0"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Web and mobile development are two of the most popular programming paths. Application development is often split into two parts- the front end and the back end. The front end of an application is the part that you can see, like the interface of a web app. The back end is what you can&rsquo;t see, the part that provides the front end with data. Some companies will have a team dedicated to the front end, and a team dedicated to the back end. Other companies only hire &ldquo;full stack&rdquo; developers that can work on both. However, this only applies to application development ie. building web or mobile apps. There are all kinds of other programming areas you can work in, like security, platform engineering and data science. To learn more about the different areas of programming, go to sites listing programming jobs, and read the job descriptions. Look at the requirements for the different jobs as well as the technologies they use. This will give you an idea of the types of things you need to learn. </span></p><h3 class="c1 c13" id="h.qb9qvyl62p21"><span>Getting an Interview</span></h3><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Once you have some experience with either open source or freelance work, it&#39;s time to start interviewing. I&rsquo;ve found the most effective way to get an interview is to focus on LinkedIn. If you don&rsquo;t have a LinkedIn account, you should create one. Add a summary about yourself at the top of of your profile, and make sure to highlight your programming skills. For example a lot of people add something like &ldquo;Languages: Python, JavaScript&rdquo;, which helps them get found by recruiters searching for these keywords. Make sure to add your open source or freelancing experience as your most recent job. </span></p><p class="c0"><span class="c3">Start connecting with technical recruiters. There are a million technical recruiters on LinkedIn, so find them and send them a request to connect. They are always looking for new talent, so they will be eager to connect with you. Once they accept your invitation, reach out and ask if they have any open positions available. </span></p><h3 class="c1 c13" id="h.noytbq5dh2oc"><span>The Interview</span></h3><p class="c1"><span class="c3 c5">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">Passing the programming interview is the hardest part of this process. Programming interviews are notoriously hard. They often consist of three rounds.</span></p><p class="c0"><span class="c3">In the first round, you speak with a recruiter over the phone. They ask you about your previous experience and skills, and try to determine if you are a good fit culturally. Generally, there are no technical questions in this initial interview, but sometimes there are, so make sure you are prepared. </span></p><p class="c0"><span class="c3">Once you make it past the first round, you will advance to the second round. The second round is typically a technical phone screen. You will speak with one or more members of the engineering team, and they will ask you similar questions about your experience and skills as the first interview. This time however, the questions are usually followed by a technical test over the phone. The engineers will most likely give you the address of website where you can both edit code. They will paste a programming question into the editor and ask you to solve it. </span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;If you make it past the second round, and this nerve racking process hasn&rsquo;t caused you to abandon this book and quit programming, you will have a third interview. The third interview is usually &ldquo;on site&rdquo; which means it&rsquo;s in person at the companies office. The third round is a lot like the first two. You meet with different engineers on the team, they ask you about your skills and experience, and there are more technical tests. Sometimes you even stay for lunch to see how you interact with the team. The third round is where the famous white board coding tests happen. If the company you are interviewing for does whiteboarding, you will be asked several programming problems and asked to solve them on a whiteboard. </span></p><h3 class="c1 c13" id="h.sgjw9ft6t6ff"><span>Hacking The Interview</span></h3><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;The majority of programming interviews focus on one subject - data structures and algorithms. That means to pass your programming interview, you know exactly what you must do- get very good at one specific area of computer science. Fortunately, this will also help you to become a better programmer. </span></p><p class="c0"><span class="c3">You can narrow down the questions you should focus on even further by thinking about the interview from the interviewer&#39;s perspective. Think about the situation your interviewer is in. They say software is never finished, and it&rsquo;s true. Your interviewer most likely has a lot of work he needs to get done, and doesn&rsquo;t want to dedicate a lot of his time to interview candidates. Coming up with good programming questions is hard. Is he going to spend his valuable time coming up with his own programming questions? Probably not. He is most likely going to google &ldquo;programming interview questions&rdquo;. The point is, the same questions come up over and over again. There are some great resources out there specifically designed to help you pass your programming interview. Leetcode is one I recommend you check out. Almost every question I&rsquo;ve ever been asked in a programming interview is on Leetcode. </span></p><p class="c0 c9"><span class="c3"></span></p><h2 class="c1 c13" id="h.ur98wqjimxe3"><span>Working on a Team</span></h2><p class="c1"><span class="c3">&nbsp;&ldquo;You can&rsquo;t have great software without a great team, and most software teams behave like dysfunctional families.&rdquo;</span></p><p class="c1"><span class="c3">- Jim McCarthy</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Coming from a self-taught background, you are probably used to working alone. Once you join a company, you will need to learn how to work on a team. Even if you start your own company, eventually you will need to hire additional programmers, at which point you will also need to learn to work as a team. </span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;As I discussed in the chapter Version Control, programming is a team sport. Like any other team sport, in programming, you need to get along with your teammates. This chapter goes over some tips for working successfully in a team environment.</span></p><h3 class="c1 c13" id="h.scwvb06zh0cg"><span>Master the Basics</span></h3><p class="c1"><span class="c3 c5">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">When you are hired, you are expected to be able to program, as well as competent in the skills covered in this book. It is not enough to simply read this book. You need to spend a lot of time continuing to learn the skills covered. Start a side project with a friend and use version control. Write new tests everyday. Schedule programs to run. If your teammates have to constantly help you with the basics, they will become frustrated. </span></p><h3 class="c1 c13" id="h.2q5gsq885ss1"><span>Don&rsquo;t Ask What You Can Google</span></h3><p class="c1"><span class="c3">As a new, self-taught member of a programming team, you will have plenty to learn, and you will need to ask a lot of questions. This is a great way to learn. However, you want to make sure you are only asking good questions. You should only ask someone a question if you&rsquo;ve spent at least five minutes trying to Google the answer. While asking questions is a positive thing, if you ask too many questions you will annoy your teammates. Make sure you are only asking good questions by trying to find the answer for at least five minutes before you ask it. </span></p><h3 class="c1 c13" id="h.rtv34luwjp6m"><span>Changing Code</span></h3><p class="c1"><span class="c3 c5">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">Another problem can happen at the opposite end of the spectrum of the previous problems I&rsquo;ve described. By reading this book you&rsquo;ve demonstrated you are the type of person that is constantly looking to improve. Unfortunately, not everyone on your team will share your enthusiasm for becoming a better programmer. </span></p><p class="c1"><span class="c3">Many people are fine no longer learning, and doing things suboptimally. This can be especially prevalent in startups, where shipping code fast is often more important than shipping high quality code. If this is the case, you should listen to Walter White and &ldquo;tread lightly&rdquo;. Programmers can get their egos hurt very easily when you change their code. Even worse, if you spend a lot of time fixing other people&rsquo;s code, you will not have enough time to contribute to new projects, and it may look like you are not working hard enough. The best defense for this is to carefully question any company you join about their engineering culture. If you still find yourself in this position, avoid the temptation to spend all of your time fixing poorly written code. Mike Coutermarsh wrote an article on Medium called Jr. Developers #5: How to Improve Code Without Anyone Hating You in which he suggests to mostly spend your time on developing new features and spending a small percentage of your time making small incremental improvements. </span></p><p class="c1"><span class="c3">As a new programmer entering your first programming job, it&#39;s important for you to get along with your team. Alienating your team members can happen much easier than it may seem, so make sure to always stay cognizant of how other teammates will react to any criticisms you may have, or changes you want to make. </span></p><h3 class="c1 c13" id="h.4tic2b1lo48c"><span>Imposter Syndrome</span></h3><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Everyone in Computer Science feels overwhelmed at times. No matter how hard you work, there are going to be things you don&rsquo;t know. As a self-taught programmer, it is especially easy to fall victim to feeling inadequate because someone asked you to do something you&rsquo;ve never heard of, or because you feel like there are so many concepts in Computer Science you still do not understand. It is important to know this happens to everyone, not just you. I was surprised when even my friend with a masters degrees in Computer Science from Stanford told me he feels this way as well. &nbsp;He told me everyone in his class felt this way, but he noticed people in his program reacted one of two ways. They either stayed humble and were always willing to admit when they didn&rsquo;t know something, and to try to learn it, or they pretended they knew everything and stifled their learning. </span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Make sure you remember you got to where you are by working hard, and it&rsquo;s ok if you don&rsquo;t know everything, nobody does. Just stay humble, relentlessly study anything you don&rsquo;t understand, and you will be unstoppable.</span></p><h2 class="c1 c13" id="h.8x7jkdckc5d9"><span>Further Learning</span></h2><p class="c1"><span class="c3">&ldquo;The best programmers are not marginally better than merely good ones. They are an order-of-magnitude better, measured by whatever standard: conceptual creativity, speed, ingenuity of design, or problem-solving ability.&rdquo;</span></p><p class="c1"><span class="c3">- Randall E. Stross</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;The article </span><span class="c15 c3">ABC: Always Be Coding</span><span class="c3">&nbsp;on Medium gives great advice on how to get a job as a software engineer. The article is summarized in the title- always be coding. If you combine ABC with the slightly tweaked expression ABL- always be learning, you are sure to have an exceptional career. &nbsp;</span></p><p class="c1"><span class="c3">&nbsp;In this chapter, I &nbsp;am going to quickly go over some of the programming resources I&rsquo;ve found most helpful. </span></p><h3 class="c1 c13" id="h.zcvgns7quxw8"><span>The Classics</span></h3><p class="c0"><span class="c3">You should read </span><span class="c15 c3">The Pragmatic Programmer</span><span class="c3">&nbsp;by Andy Hunt and Dave Thomas as well as </span><span class="c15 c3">Design Patterns</span><span class="c3">&nbsp;by Erich Gamma, John Vlissides, Ralph Johnson, and Richard Helm. These books are considered classics in the programming community, and you are more or less expected to have read them. </span></p><h3 class="c1 c13" id="h.gafsiilgv7q3"><span>Replacing Classics</span></h3><p class="c0"><span class="c3">There are a few classics I left out of the previous section. These include </span><span class="c15 c3">Code Complete</span><span class="c3">&nbsp;by Steve McConnell, </span><span class="c3 c15">Compilers: Principles, Techniques, and Tools, </span><span class="c3">by Alfred Aho, Jeffrey Ullman, Monica S. Lam, and Ravi Sethi and </span><span class="c15 c3">Introduction to Algorithms</span><span class="c3">&nbsp;by The MIT Press. </span></p><p class="c1"><span class="c3">The reason I left these out is because they are all around 1000 pages. I&rsquo;ve tried and failed to read all of them. On one of his podcasts, Tim Ferriss said something along the lines of &ldquo;The best recommendation is the one you will actually follow through with&rdquo;. If you have the time and discipline to make your way through these books, you should do so. If you find yourself unable to finish these books, you need to get the information another way. </span></p><p class="c0"><span class="c3">I recommend replacing these books with other resources you are much more likely to finish, but will get you close to the same results. </span><span class="c15 c3">Problem Solving with Data Structures and Algorithms</span><span class="c3">&nbsp;is a free, interactive, excellent introduction to algorithms, much easier to understand than </span><span class="c15 c3">MIT&rsquo;s Introduction to Algorithms</span><span class="c3">. </span></p><h3 class="c1 c13" id="h.5pn903in0jyx"><span>CodeSchool</span></h3><p class="c0"><span class="c3">Online coding classes are a popular way to learn to program. Coursera is one of the most popular, but I personally use the lesser known Codeschool. I highly recommend you take some of their classes. Not only do they have great classes on different programming languages, but they also have classes on Git, SQL and other subjects discussed in this book. </span></p><h3 class="c1 c13" id="h.3khufknsvlxz"><span>Hacker News</span></h3><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Hacker News is a platform for user submitted news on the technology incubator Y Combinators website. It is popular among programmers and will help you keep up to date with the newest trends and technologies. </span></p><h3 class="c1 c13" id="h.54jkghjmx169"><span>Other</span></h3><p class="c1"><span class="c17 c3"><a class="c18" href="https://www.google.com/url?q=http://matt.might.net/articles/what-cs-majors-should-know/&amp;sa=D&amp;ust=1467337427609000&amp;usg=AFQjCNFcYGVj4RWLHD8KcYKj3OOY7XiDQA">http://matt.might.net/articles/what-cs-majors-should-know/</a></span></p><p class="c1"><span class="c17 c3"><a class="c18" href="https://www.google.com/url?q=http://steve-yegge.blogspot.co.id/2006/03/execution-in-kingdom-of-nouns.html&amp;sa=D&amp;ust=1467337427609000&amp;usg=AFQjCNHSgFf78MeiZnVMPIt24Y0LPgTKfg">http://steve-yegge.blogspot.co.id/2006/03/execution-in-kingdom-of-nouns.html</a></span></p><p class="c1"><span class="c17 c3"><a class="c18" href="https://www.google.com/url?q=http://gribblelab.org/CBootcamp/7_Memory_Stack_vs_Heap.html&amp;sa=D&amp;ust=1467337427609000&amp;usg=AFQjCNFLIuWVhfjqob1UxTpEgUwdQmN12Q">http://gribblelab.org/CBootcamp/7_Memory_Stack_vs_Heap.html</a></span></p><p class="c1"><span class="c17 c3"><a class="c18" href="https://www.google.com/url?q=http://stackoverflow.com/questions/10925478/how-to-read-api-documentation-for-newbs&amp;sa=D&amp;ust=1467337427610000&amp;usg=AFQjCNH5qx3ohbxgEk4fJEaskU7AjNkg2g">http://stackoverflow.com/questions/10925478/how-to-read-api-documentation-for-newbs</a></span></p><p class="c1"><span class="c17 c3"><a class="c18" href="https://www.google.com/url?q=http://interactivepython.org/runestone/static/pythonds/index.html&amp;sa=D&amp;ust=1467337427610000&amp;usg=AFQjCNExL5M7311s4Qp1vNWsUh0XHrBo-A">http://interactivepython.org/runestone/static/pythonds/index.html</a></span></p><p class="c1"><span class="c17 c3"><a class="c18" href="https://www.google.com/url?q=http://stackoverflow.com/questions/79923/what-and-where-are-the-stack-and-heap&amp;sa=D&amp;ust=1467337427610000&amp;usg=AFQjCNFV0jEL_5Ae637Ibegg-82pFn56NA">http://stackoverflow.com/questions/79923/what-and-where-are-the-stack-and-heap</a></span></p><p class="c1"><span class="c17 c3"><a class="c18" href="https://www.google.com/url?q=http://preethikasireddy.me/?p%3D174&amp;sa=D&amp;ust=1467337427613000&amp;usg=AFQjCNHOTGO52K1be1Og0qTt6wS4bospQw">http://preethikasireddy.me/?p=174</a></span></p><p class="c1"><span class="c17 c3"><a class="c18" href="https://www.google.com/url?q=http://berb.github.io/diploma-thesis/index.html&amp;sa=D&amp;ust=1467337427613000&amp;usg=AFQjCNG0mhl9rlpicA2YDdYJWdvCpqEVgg">http://berb.github.io/diploma-thesis/index.html</a></span></p><p class="c1 c9"><span></span></p><h2 class="c1 c13" id="h.bty722mwqr97"><span>Next Steps</span></h2><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;First of all, thank you for purchasing this book. I hope it&rsquo;s helped you become a better programmer. The programming community has given me so much support and by writing this book, I hope I&rsquo;ve managed to give help others learning to program the way I was helped. If this book helps anyone to further their programming career, I will have achieved my goal for writing this book.</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Now that you&rsquo;ve finished, &nbsp;it&#39;s time for you to get down to business. Where do you go from here? Algorithms, algorithms algorithms. Get on LeetCode and practice those algorithms. Then practice them some more! This chapter gives you advice for how to continue to improve as a programmer, once you finished practicing your algorithms. </span></p><h3 class="c1 c13" id="h.inzb4q6f2txm"><span>Find a Mentor</span></h3><p class="c1"><span class="c3 c5">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">Finding a mentor will help you take your programming skills to the next level. As I mentioned before, one of the hard things about learning to program is that you can be doing things wrong without even knowing it. I mentioned earlier in the book that you help combat this by doing code reviews. A mentor can take this a step further and help you improve your coding process, recommend books, and it&#39;s helpful to have someone to talk to about programming concepts you are having trouble understanding. </span></p><h3 class="c1 c13" id="h.2w3b4apad3y9"><span>Strive to Go Deep</span></h3><p class="c1"><span class="c3 c5">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c3">&nbsp;In programming, there is a concept called a &ldquo;black box&rdquo;. It refers to something you often use, but do not understand how it works. One of my friends told me it was a major &ldquo;aha&rdquo; moment for them when they realized the command line is a program itself. </span></p><p class="c1"><span class="c3">When you first start programming, everything is a black box. Command line programs are a black box. One of the best ways to get better at programming is to try to open up every black box you can find and try to understand how it works. In this chapter we are going to open up some black boxes by going over executable program. &nbsp;The best way to become a better programmer is to go deep.</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Writing this book helped me go deep. There were certain concepts I thought I understood, only to find out I couldn&rsquo;t explain them. I had to go deeper. Don&rsquo;t just stop at one explanation, read all the explanations on the topic you can find. Ask questions and read differing opinions online.</span></p><p class="c1"><span class="c3">&nbsp;I find one of the most helpful questions to be &ldquo;What problem does this solve?&rdquo;. For example, we learned about object-oriented programming in this book. But why did we learn about it? Why was object-oriented programming invented? What problem does it solve? Pursuing the answers to these types of questions will help you become a better programmer. </span></p><p class="c1"><span class="c3">Another way to go deep is by building things you want to understand better. Having trouble understanding databases? Build a simple database in your free time. When I was having trouble understanding compilers, I built a simple lisp compiler. Taking the time to do a project like this is well worth the investment, as it will greatly improve your understanding of whatever concept you are struggling with. </span></p><h3 class="c1 c13" id="h.w24wxpj7iuxu"><span>Other Advice</span></h3><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;I once came across a forum discussing ways to become a better programmer. The top voted answer was a somewhat surprising &ldquo;Do things other than programming.&rdquo;. In a similar vein, I recommend reading </span><span class="c15 c3">The Talent Code</span><span class="c3">&nbsp;by Daniel Coyle because he lays out exactly what you need to do to master any skill.</span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Reading code is one of the best ways to get better at programming. When you are learning, you need to make sure to strike a balance between writing code, and reading code. Even though you should be able to read your own code now, reading other people&#39;s code is still going to be difficult at first. None the less, it is important to read other people&rsquo;s code anyway because you can see what you are doing wrong, and being able to read and work on code you didn&rsquo;t write is a fundamental aspect of programming. In this chapter we will go over some strategies for reading code you didn&rsquo;t write. </span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">Please feel free to reach me at cory@theselftaughtprogrammer.io for any reason. I also run a programming newsletter you can sign up for at theselftaughprogrammer.io </span></p><h3 class="c1 c13 c107" id="h.g55xkhi375fk"><span class="c3"></span></h3><h2 class="c1 c13" id="h.5qao7ub1zt7x"><span>Acknowledgements</span><sup><a href="#cmnt73" id="cmnt_ref73">[bu]</a></sup></h2><p class="c1"><span>parents, Pam, Randee, Anzar, Cover Designer</span></p><p class="c1"><span>Citations</span></p><ol class="c45 lst-kix_t5715nnbapc2-0 start" start="1"><li class="c1 c10 c38"><span class="c17 c3"><a class="c18" href="https://www.google.com/url?q=http://stackoverflow.com/questions/466790/assembly-code-vs-machine-code-vs-object-code&amp;sa=D&amp;ust=1467337427618000&amp;usg=AFQjCNHYTZvNgMGAicjfL2xLXHpgu_egLA">http://stackoverflow.com/questions/466790/assembly-code-vs-machine-code-vs-object-code</a></span></li><li class="c1 c9 c10 c38"><span class="c17 c3"><a class="c18" href="https://www.google.com/url?q=http://stackoverflow.com/questions/466790/assembly-code-vs-machine-code-vs-object-code&amp;sa=D&amp;ust=1467337427619000&amp;usg=AFQjCNHH6u4x7NcES4SQX9A3hKHd8eFgfg"></a></span></li><li class="c1 c10 c38"><span class="c3">http://man7.org/linux/man-pages/man1/ls.1.html</span></li><li class="c1 c9 c10 c38"><span class="c3"></span></li><li class="c1 c10 c38"><span class="c17 c3"><a class="c18" href="https://www.google.com/url?q=http://www.webopedia.com/TERM/C/clock_speed.html&amp;sa=D&amp;ust=1467337427619000&amp;usg=AFQjCNFAa6OUhZCtSGEwin0OJKuguOYZoA">http://www.webopedia.com/TERM/C/clock_speed.html</a></span></li><li class="c1 c9 c10 c38"><span class="c17 c3"><a class="c18" href="https://www.google.com/url?q=http://www.webopedia.com/TERM/C/clock_speed.html&amp;sa=D&amp;ust=1467337427619000&amp;usg=AFQjCNFAa6OUhZCtSGEwin0OJKuguOYZoA"></a></span></li><li class="c1 c10 c38"><span class="c17 c3"><a class="c18" href="https://www.google.com/url?q=http://stackoverflow.com/questions/1050222/concurrency-vs-parallelism-what-is-the-difference&amp;sa=D&amp;ust=1467337427620000&amp;usg=AFQjCNHZ_nQKX4uRGXrTRN4hCFzQHEYMpQ">http://stackoverflow.com/questions/1050222/concurrency-vs-parallelism-what-is-the-difference</a></span></li><li class="c1 c9 c10 c38"><span class="c17 c3"><a class="c18" href="https://www.google.com/url?q=http://stackoverflow.com/questions/1050222/concurrency-vs-parallelism-what-is-the-difference&amp;sa=D&amp;ust=1467337427620000&amp;usg=AFQjCNHZ_nQKX4uRGXrTRN4hCFzQHEYMpQ"></a></span></li><li class="c1 c10 c38"><span class="c17 c3"><a class="c18" href="https://www.google.com/url?q=http://berb.github.io/diploma-thesis/original/022_webapps.html&amp;sa=D&amp;ust=1467337427621000&amp;usg=AFQjCNFWIxBNwItNNpjVFtW7txKjqnBbWw">http://berb.github.io/diploma-thesis/original/022_webapps.html</a></span></li><li class="c1 c9 c10 c38"><span class="c17 c3"><a class="c18" href="https://www.google.com/url?q=http://berb.github.io/diploma-thesis/original/022_webapps.html&amp;sa=D&amp;ust=1467337427621000&amp;usg=AFQjCNFWIxBNwItNNpjVFtW7txKjqnBbWw"></a></span></li><li class="c1 c10 c38"><span class="c17 c3"><a class="c18" href="https://www.google.com/url?q=http://stackoverflow.com/questions/11828270/how-to-exit-the-vim-editor&amp;sa=D&amp;ust=1467337427621000&amp;usg=AFQjCNGXV41XywB_KzJ0W_tSnW51xL3DBw">http://stackoverflow.com/questions/11828270/how-to-exit-the-vim-editor</a></span></li><li class="c1 c9 c10 c38"><span class="c17 c3"><a class="c18" href="https://www.google.com/url?q=http://stackoverflow.com/questions/11828270/how-to-exit-the-vim-editor&amp;sa=D&amp;ust=1467337427622000&amp;usg=AFQjCNH7iGOWl07xZ_7HOilGb9xdysOarw"></a></span></li><li class="c1 c10 c38"><span class="c17 c3"><a class="c18" href="https://www.google.com/url?q=http://superuser.com/questions/666942/why-it-is-not-recommend-to-use-root-login-in-linux&amp;sa=D&amp;ust=1467337427622000&amp;usg=AFQjCNGoiuhuIjKZDJHFs746xWV8BDUEyA">http://superuser.com/questions/666942/why-it-is-not-recommend-to-use-root-login-in-linux</a></span></li><li class="c1 c9 c10 c38"><span class="c17 c3"><a class="c18" href="https://www.google.com/url?q=http://superuser.com/questions/666942/why-it-is-not-recommend-to-use-root-login-in-linux&amp;sa=D&amp;ust=1467337427622000&amp;usg=AFQjCNGoiuhuIjKZDJHFs746xWV8BDUEyA"></a></span></li><li class="c1 c10 c38"><span class="c17 c3"><a class="c18" href="https://www.google.com/url?q=http://programmers.stackexchange.com/questions/37294/logging-why-and-what&amp;sa=D&amp;ust=1467337427623000&amp;usg=AFQjCNEY4j41tHI7dVaN1Fq9Xsjq9J8BbQ">http://programmers.stackexchange.com/questions/37294/logging-why-and-what</a></span></li><li class="c1 c9 c10 c38"><span class="c17 c3"><a class="c18" href="https://www.google.com/url?q=http://programmers.stackexchange.com/questions/37294/logging-why-and-what&amp;sa=D&amp;ust=1467337427623000&amp;usg=AFQjCNEY4j41tHI7dVaN1Fq9Xsjq9J8BbQ"></a></span></li><li class="c1 c10 c38"><span class="c17 c3"><a class="c18" href="https://www.google.com/url?q=http://stackoverflow.com/questions/10925478/how-to-read-api-documentation-for-newbs&amp;sa=D&amp;ust=1467337427623000&amp;usg=AFQjCNGCSoQcyhRULxRDtqlPxbzcGibrYw">http://stackoverflow.com/questions/10925478/how-to-read-api-documentation-for-newbs</a></span></li><li class="c1 c9 c10 c38"><span class="c17 c3"><a class="c18" href="https://www.google.com/url?q=http://stackoverflow.com/questions/10925478/how-to-read-api-documentation-for-newbs&amp;sa=D&amp;ust=1467337427623000&amp;usg=AFQjCNGCSoQcyhRULxRDtqlPxbzcGibrYw"></a></span></li><li class="c1 c10 c38"><span class="c17 c3"><a class="c18" href="https://www.google.com/url?q=http://www.infoworld.com/article/2908474/application-development/stack-overflow-survey-finds-nearly-half-have-no-degree-in-computer-science.html&amp;sa=D&amp;ust=1467337427624000&amp;usg=AFQjCNEmz_RXsMCM0xGgThJzU4oh1ZgCLQ">http://www.infoworld.com/article/2908474/application-development/stack-overflow-survey-finds-nearly-half-have-no-degree-in-computer-science.html</a></span></li><li class="c1 c9 c10 c38"><span class="c17 c3"><a class="c18" href="https://www.google.com/url?q=http://www.infoworld.com/article/2908474/application-development/stack-overflow-survey-finds-nearly-half-have-no-degree-in-computer-science.html&amp;sa=D&amp;ust=1467337427624000&amp;usg=AFQjCNEmz_RXsMCM0xGgThJzU4oh1ZgCLQ"></a></span></li><li class="c1 c10 c38"><span class="c17 c3"><a class="c18" href="https://www.google.com/url?q=http://www.merriam-webster.com/dictionary/catenate&amp;sa=D&amp;ust=1467337427625000&amp;usg=AFQjCNGIPb70jRPXnxQmFQNhTNZMh_Gyhw">http://www.merriam-webster.com/dictionary/catenate</a></span></li><li class="c1 c9 c10 c38"><span class="c17 c3"><a class="c18" href="https://www.google.com/url?q=http://www.merriam-webster.com/dictionary/catenate&amp;sa=D&amp;ust=1467337427625000&amp;usg=AFQjCNGIPb70jRPXnxQmFQNhTNZMh_Gyhw"></a></span></li><li class="c1 c10 c38"><span class="c3">Design Patterns kindle location 546</span></li><li class="c1 c9 c10 c38"><span class="c3"></span></li><li class="c1 c10 c38"><span class="c3">Design Patterns kindle location 650</span></li><li class="c1 c9 c10 c38"><span class="c3"></span></li><li class="c1 c10 c38"><span class="c3">Design Patterns kindle location 824</span></li><li class="c1 c9 c10 c38"><span class="c3"></span></li><li class="c1 c10 c38"><span class="c17 c3"><a class="c18" href="https://www.google.com/url?q=https://hungred.com/wp-content/uploads/2009/05/ascii-table-cheat-sheet1.png&amp;sa=D&amp;ust=1467337427626000&amp;usg=AFQjCNEW13xOySrVp7yCTEzAN9ha3KIEKg">https://hungred.com/wp-content/uploads/2009/05/ascii-table-cheat-sheet1.png</a></span></li><li class="c1 c9 c10 c38"><span class="c17 c3"><a class="c18" href="https://www.google.com/url?q=https://hungred.com/wp-content/uploads/2009/05/ascii-table-cheat-sheet1.png&amp;sa=D&amp;ust=1467337427627000&amp;usg=AFQjCNH1IhbvB4fFFxrJ6EM-5ardfiV2-A"></a></span></li><li class="c1 c10 c38"><span class="c3">The Pragmatic Programmer kindle location 830</span></li><li class="c1 c9 c10 c38"><span class="c3"></span></li><li class="c1 c10 c38"><span class="c17 c3"><a class="c18" href="https://www.google.com/url?q=https://automatetheboringstuff.com/chapter0/&amp;sa=D&amp;ust=1467337427627000&amp;usg=AFQjCNHVLagTQVEGk_ZqkMp6Yl1IX7YTzQ">https://automatetheboringstuff.com/chapter0/</a></span></li><li class="c1 c9 c10 c38"><span class="c17 c3"><a class="c18" href="https://www.google.com/url?q=https://automatetheboringstuff.com/chapter0/&amp;sa=D&amp;ust=1467337427627000&amp;usg=AFQjCNHVLagTQVEGk_ZqkMp6Yl1IX7YTzQ"></a></span></li><li class="c1 c10 c38"><span class="c17 c3"><a class="c18" href="https://www.google.com/url?q=http://kevinlondon.com/2015/07/26/dangerous-python-functions.html&amp;sa=D&amp;ust=1467337427628000&amp;usg=AFQjCNFPULvgA6dih45qbnNatFyiL4pipw">http://kevinlondon.com/2015/07/26/dangerous-python-functions.html</a></span></li><li class="c1 c9 c10 c38"><span class="c17 c3"><a class="c18" href="https://www.google.com/url?q=http://kevinlondon.com/2015/07/26/dangerous-python-functions.html&amp;sa=D&amp;ust=1467337427628000&amp;usg=AFQjCNFPULvgA6dih45qbnNatFyiL4pipw"></a></span></li><li class="c1 c10 c38"><span class="c17 c3"><a class="c18" href="https://www.google.com/url?q=http://interactivepython.org/runestone/static/pythonds/Recursion/TheThreeLawsofRecursion.html&amp;sa=D&amp;ust=1467337427628000&amp;usg=AFQjCNFPfuDaIlElFXx3s-JULWo6SwE9Wg">http://interactivepython.org/runestone/static/pythonds/Recursion/TheThreeLawsofRecursion.html</a></span></li><li class="c1 c9 c10 c38"><span class="c17 c3"><a class="c18" href="https://www.google.com/url?q=http://interactivepython.org/runestone/static/pythonds/Recursion/TheThreeLawsofRecursion.html&amp;sa=D&amp;ust=1467337427629000&amp;usg=AFQjCNEyTtwSgujd4Pvmpbwb1fBA3MsVkQ"></a></span></li><li class="c1 c10 c38"><span class="c3">https://automatetheboringstuff.com/chapter1/</span></li><li class="c1 c10 c38"><span class="c17 c3"><a class="c18" href="https://www.google.com/url?q=https://www.quora.com/What-are-some-interesting-facts-about-computer-programming&amp;sa=D&amp;ust=1467337427629000&amp;usg=AFQjCNFRKOlpOb6eWbr7bSgySCUzw8mp7w">https://www.quora.com/What-are-some-interesting-facts-about-computer-programming</a></span></li><li class="c1 c10 c38"><span class="c3">&nbsp;</span><span class="c17 c3"><a class="c18" href="https://www.google.com/url?q=http://www.dkfindout.com/us/explore/eight-cool-facts-about-computer-coding/&amp;sa=D&amp;ust=1467337427630000&amp;usg=AFQjCNHGXbcKToeQV0McqOm6oagcWBRsyA">http://www.dkfindout.com/us/explore/eight-cool-facts-about-computer-coding/</a></span></li><li class="c1 c10 c38"><span class="c17 c3"><a class="c18" href="https://www.google.com/url?q=http://thenextweb.com/insider/2016/02/26/8-facts-every-computer-programmer-should-know/%23gref&amp;sa=D&amp;ust=1467337427630000&amp;usg=AFQjCNHCxAOkcFVb2Wz0RQtONzrS8a7_eg">http://thenextweb.com/insider/2016/02/26/8-facts-every-computer-programmer-should-know/#gref</a></span></li><li class="c1 c10 c38"><span class="c17 c3"><a class="c18" href="https://www.google.com/url?q=http://cs.lmu.edu/~ray/notes/x86assembly/&amp;sa=D&amp;ust=1467337427631000&amp;usg=AFQjCNEY0AYoVzPoTxYS6Bm1vF75srSBng">http://cs.lmu.edu/~ray/notes/x86assembly/</a></span></li><li class="c1 c10 c38"><span class="c17 c3"><a class="c18" href="https://www.google.com/url?q=http://www.tutorialspoint.com/python/python_files_io.htm&amp;sa=D&amp;ust=1467337427631000&amp;usg=AFQjCNGUJsWvAnEoFOoceMitU1WTHu3vig">http://www.tutorialspoint.com/python/python_files_io.htm</a></span></li><li class="c1 c10 c38"><span class="c17 c3"><a class="c18" href="https://www.google.com/url?q=http://www.wsj.com/articles/computer-programming-is-a-trade-lets-act-like-it-1407109947?mod%3De2fb&amp;sa=D&amp;ust=1467337427632000&amp;usg=AFQjCNHi4Kqv0qNnkrP81EizCN-EW6T5wQ">http://www.wsj.com/articles/computer-programming-is-a-trade-lets-act-like-it-1407109947?mod=e2fb</a></span></li><li class="c1 c10 c38"><span class="c17 c3"><a class="c18" href="https://www.google.com/url?q=https://en.wikipedia.org/wiki/Persistence_(computer_science)&amp;sa=D&amp;ust=1467337427632000&amp;usg=AFQjCNFVHpW168Yf4BVHmOtwdq5ud4zZnA">https://en.wikipedia.org/wiki/Persistence_(computer_science)</a></span></li><li class="c1 c10 c38"><span class="c17 c3"><a class="c18" href="https://www.google.com/url?q=https://en.wikipedia.org/wiki/Column_family&amp;sa=D&amp;ust=1467337427633000&amp;usg=AFQjCNHxLjmndSUInRJ9-RSgUCT2mvdYLQ">https://en.wikipedia.org/wiki/Column_family</a></span></li><li class="c1 c10 c38"><span class="c17 c3"><a class="c18" href="https://www.google.com/url?q=http://stackoverflow.com/questions/2570756/what-are-database-constraints&amp;sa=D&amp;ust=1467337427633000&amp;usg=AFQjCNHg0PEkp8JUNDwpk-rta_N-z7jWjg">http://stackoverflow.com/questions/2570756/what-are-database-constraints</a></span></li><li class="c1 c10 c38"><span class="c17 c3"><a class="c18" href="https://www.google.com/url?q=https://en.wikipedia.org/wiki/Data_integrity&amp;sa=D&amp;ust=1467337427633000&amp;usg=AFQjCNEstv6uY7_UjD_Z5FyGjBlGu0vJ2A">https://en.wikipedia.org/wiki/Data_integrity</a></span></li><li class="c1 c10 c38"><span class="c17 c3"><a class="c18" href="https://www.google.com/url?q=https://www.sitepoint.com/understanding-the-observer-pattern/&amp;sa=D&amp;ust=1467337427634000&amp;usg=AFQjCNG0ULQz2vO3G5OBMLHQUL639qXJHA">https://www.sitepoint.com/understanding-the-observer-pattern/</a></span></li><li class="c1 c10 c38"><span class="c17 c3"><a class="c18" href="https://www.google.com/url?q=http://codedx.com/how-to-minimize-your-softwares-attack-surface/&amp;sa=D&amp;ust=1467337427634000&amp;usg=AFQjCNENv0Yz7vtHZqnaTjya7zc_e-ofCA">http://codedx.com/how-to-minimize-your-softwares-attack-surface/</a></span></li><li class="c1 c10 c38"><span class="c17 c3"><a class="c18" href="https://www.google.com/url?q=https://support.microsoft.com/en-us/kb/283878&amp;sa=D&amp;ust=1467337427635000&amp;usg=AFQjCNE_TjMzIMEqzBxC4OXeUByCdhEuSQ">https://support.microsoft.com/en-us/kb/283878</a></span></li><li class="c1 c10 c38"><span class="c17 c3"><a class="c18" href="https://www.google.com/url?q=http://www.slideshare.net/jagaarj/database-design-normalization&amp;sa=D&amp;ust=1467337427635000&amp;usg=AFQjCNFBlk83whPu_rcNMncaA86OCg2JBg">http://www.slideshare.net/jagaarj/database-design-normalization</a></span></li><li class="c1 c10 c38"><span class="c3">http://www.tcpipguide.com/free/t_WhatIsNetworking.htm</span></li></ol><p class="c1 c9"><span class="c3"></span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1"><span class="c3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p><p class="c1 c9"><span class="c3"></span></p><p class="c1 c9"><span class="c3"></span></p><div><p class="c1 c9 c90"><span></span></p></div><div class="c19"><p class="c21"><a href="#cmnt_ref1" id="cmnt1">[a]</a><span class="c6">TK</span></p></div><div class="c19"><p class="c21"><a href="#cmnt_ref2" id="cmnt2">[b]</a><span class="c6">TK</span></p></div><div class="c19"><p class="c21"><a href="#cmnt_ref3" id="cmnt3">[c]</a><span class="c6">put more detailed instructions on website</span></p></div><div class="c19"><p class="c21"><a href="#cmnt_ref4" id="cmnt4">[d]</a><span class="c6">and include video</span></p></div><div class="c19"><p class="c21"><a href="#cmnt_ref5" id="cmnt5">[e]</a><span class="c6">add linux</span></p></div><div class="c19"><p class="c21"><a href="#cmnt_ref6" id="cmnt6">[f]</a><span class="c6">new explanation explaining shell vs non shell, GitHub and copying and pasting. Let me know what you think!</span></p></div><div class="c19"><p class="c21"><a href="#cmnt_ref7" id="cmnt7">[g]</a><span class="c6">new section explaining triple string comments</span></p></div><div class="c19"><p class="c21"><a href="#cmnt_ref8" id="cmnt8">[h]</a><span class="c6">create standard for carrying over code</span></p></div><div class="c19"><p class="c21"><a href="#cmnt_ref9" id="cmnt9">[i]</a><span class="c6">move to earlier chapter</span></p></div><div class="c19"><p class="c21"><a href="#cmnt_ref10" id="cmnt10">[j]</a><span class="c6">make sure top of range needing one more is explained earlier</span></p></div><div class="c19"><p class="c21"><a href="#cmnt_ref11" id="cmnt11">[k]</a><span class="c6">add to earlier chapter</span></p></div><div class="c19"><p class="c21"><a href="#cmnt_ref12" id="cmnt12">[l]</a><span class="c6">fact check</span></p></div><div class="c19"><p class="c21"><a href="#cmnt_ref13" id="cmnt13">[m]</a><span class="c6">Hmmm. Is it:</span></p><p class="c21"><span class="c6">Excel uses the commas to determine where each cell ends (the commas are not displayed to the user) and each new line in the file is a new row.</span></p></div><div class="c19"><p class="c21"><a href="#cmnt_ref14" id="cmnt14">[n]</a><span class="c6">TK</span></p></div><div class="c19"><p class="c21"><a href="#cmnt_ref15" id="cmnt15">[o]</a><span class="c6">fact check</span></p></div><div class="c19"><p class="c21"><a href="#cmnt_ref16" id="cmnt16">[p]</a><span class="c6">ck indent etc in HTML</span></p></div><div class="c19"><p class="c21"><a href="#cmnt_ref17" id="cmnt17">[q]</a><span class="c6">Something was bothering me about this code yesterday, but I&#39;m sorry, the thought was lost in the fire. I&#39;ve made a note to check it again next pass through.</span></p></div><div class="c19"><p class="c21"><a href="#cmnt_ref18" id="cmnt18">[r]</a><span class="c6">tk</span></p></div><div class="c19"><p class="c21"><a href="#cmnt_ref19" id="cmnt19">[s]</a><span class="c6">Unclear. What 6? Result is 10.</span></p></div><div class="c19"><p class="c21"><a href="#cmnt_ref20" id="cmnt20">[t]</a><span class="c6">added clarification. I am talking about the number of characters in each string.</span></p></div><div class="c19"><p class="c21"><a href="#cmnt_ref21" id="cmnt21">[u]</a><span class="c6">Aren&#39;t the strings &quot;Hello&quot; and &quot;World&quot;?</span></p></div><div class="c19"><p class="c21"><a href="#cmnt_ref22" id="cmnt22">[v]</a><span class="c6">This code works fine. But I suggest using a different tree . Pines stay green all year. Maples do turn colors. Apple trees turn colors as indicated.</span></p></div><div class="c19"><p class="c21"><a href="#cmnt_ref23" id="cmnt23">[w]</a><span class="c6">Good idea!</span></p></div><div class="c19"><p class="c21"><a href="#cmnt_ref24" id="cmnt24">[x]</a><span class="c6">At first look, this code seems diff from the GitHub code. But the hill below my house is on fire. Fire truck outside my window. may need to evacuate. Taking a break.</span></p></div><div class="c19"><p class="c21"><a href="#cmnt_ref25" id="cmnt25">[y]</a><span class="c6">I will check it out. Oh man I hope everything is ok!</span></p></div><div class="c19"><p class="c21"><a href="#cmnt_ref26" id="cmnt26">[z]</a><span class="c6">I suggest checking and double-checking all the game code. Maybe get a kid to test it.</span></p></div><div class="c19"><p class="c21"><a href="#cmnt_ref27" id="cmnt27">[aa]</a><span class="c6">Edited to here, 6/28/16</span></p></div><div class="c19"><p class="c21"><a href="#cmnt_ref28" id="cmnt28">[ab]</a><span class="c6">add installing ubuntu</span></p></div><div class="c19"><p class="c21"><a href="#cmnt_ref29" id="cmnt29">[ac]</a><span class="c6">make sure unix-like is explained well earlier</span></p></div><div class="c19"><p class="c21"><a href="#cmnt_ref30" id="cmnt30">[ad]</a><span class="c6">TK</span></p></div><div class="c19"><p class="c21"><a href="#cmnt_ref31" id="cmnt31">[ae]</a><span class="c6">TK</span></p></div><div class="c19"><p class="c21"><a href="#cmnt_ref32" id="cmnt32">[af]</a><span class="c6">TK</span></p></div><div class="c19"><p class="c21"><a href="#cmnt_ref33" id="cmnt33">[ag]</a><span class="c6">TK</span></p></div><div class="c19"><p class="c21"><a href="#cmnt_ref34" id="cmnt34">[ah]</a><span class="c6">TK</span></p></div><div class="c19"><p class="c21"><a href="#cmnt_ref35" id="cmnt35">[ai]</a><span class="c6">TK</span></p></div><div class="c19"><p class="c21"><a href="#cmnt_ref36" id="cmnt36">[aj]</a><span class="c6">TK</span></p></div><div class="c19"><p class="c21"><a href="#cmnt_ref37" id="cmnt37">[ak]</a><span class="c6">explain quotes</span></p></div><div class="c19"><p class="c21"><a href="#cmnt_ref38" id="cmnt38">[al]</a><span class="c6">TK</span></p></div><div class="c19"><p class="c21"><a href="#cmnt_ref39" id="cmnt39">[am]</a><span class="c6">specific version</span></p></div><div class="c19"><p class="c21"><a href="#cmnt_ref40" id="cmnt40">[an]</a><span class="c6">consistent usage of command line v terminal</span></p></div><div class="c19"><p class="c21"><a href="#cmnt_ref41" id="cmnt41">[ao]</a><span class="c6">download a specific version</span></p></div><div class="c19"><p class="c21"><a href="#cmnt_ref42" id="cmnt42">[ap]</a><span class="c6">TK</span></p></div><div class="c19"><p class="c21"><a href="#cmnt_ref43" id="cmnt43">[aq]</a><span class="c6">TK</span></p></div><div class="c19"><p class="c21"><a href="#cmnt_ref44" id="cmnt44">[ar]</a><span class="c6">explain version</span></p></div><div class="c19"><p class="c21"><a href="#cmnt_ref45" id="cmnt45">[as]</a><span class="c6">TK</span></p></div><div class="c19"><p class="c21"><a href="#cmnt_ref46" id="cmnt46">[at]</a><span class="c6">TK</span></p></div><div class="c19"><p class="c21"><a href="#cmnt_ref47" id="cmnt47">[au]</a><span class="c6">TK</span></p></div><div class="c19"><p class="c21"><a href="#cmnt_ref48" id="cmnt48">[av]</a><span class="c6">TK</span></p></div><div class="c19"><p class="c21"><a href="#cmnt_ref49" id="cmnt49">[aw]</a><span class="c6">TK</span></p></div><div class="c19"><p class="c21"><a href="#cmnt_ref50" id="cmnt50">[ax]</a><span class="c6">TK</span></p></div><div class="c19"><p class="c21"><a href="#cmnt_ref51" id="cmnt51">[ay]</a><span class="c6">Color is a nice way to differentiate parts of code.</span></p></div><div class="c19"><p class="c21"><a href="#cmnt_ref52" id="cmnt52">[az]</a><span class="c6">TK</span></p></div><div class="c19"><p class="c21"><a href="#cmnt_ref53" id="cmnt53">[ba]</a><span class="c6">TK</span></p></div><div class="c19"><p class="c21"><a href="#cmnt_ref54" id="cmnt54">[bb]</a><span class="c6">TK</span></p></div><div class="c19"><p class="c21"><a href="#cmnt_ref55" id="cmnt55">[bc]</a><span class="c6">TK</span></p></div><div class="c19"><p class="c21"><a href="#cmnt_ref56" id="cmnt56">[bd]</a><span class="c6">TK</span></p></div><div class="c19"><p class="c21"><a href="#cmnt_ref57" id="cmnt57">[be]</a><span class="c6">TK</span></p></div><div class="c19"><p class="c21"><a href="#cmnt_ref58" id="cmnt58">[bf]</a><span class="c6">TK</span></p></div><div class="c19"><p class="c21"><a href="#cmnt_ref59" id="cmnt59">[bg]</a><span class="c6">Correct first sentence</span></p></div><div class="c19"><p class="c21"><a href="#cmnt_ref60" id="cmnt60">[bh]</a><span class="c6">TK</span></p></div><div class="c19"><p class="c21"><a href="#cmnt_ref61" id="cmnt61">[bi]</a><span class="c6">TK</span></p></div><div class="c19"><p class="c21"><a href="#cmnt_ref62" id="cmnt62">[bj]</a><span class="c6">TK</span></p></div><div class="c19"><p class="c21"><a href="#cmnt_ref63" id="cmnt63">[bk]</a><span class="c6">TK</span></p></div><div class="c19"><p class="c21"><a href="#cmnt_ref64" id="cmnt64">[bl]</a><span class="c6">TK</span></p></div><div class="c19"><p class="c21"><a href="#cmnt_ref65" id="cmnt65">[bm]</a><span class="c6">TK</span></p></div><div class="c19"><p class="c21"><a href="#cmnt_ref66" id="cmnt66">[bn]</a><span class="c6">TK</span></p></div><div class="c19"><p class="c21"><a href="#cmnt_ref67" id="cmnt67">[bo]</a><span class="c6">TK</span></p></div><div class="c19"><p class="c21"><a href="#cmnt_ref68" id="cmnt68">[bp]</a><span class="c6">TK,TK</span></p></div><div class="c19"><p class="c21"><a href="#cmnt_ref69" id="cmnt69">[bq]</a><span class="c6">TK</span></p></div><div class="c19"><p class="c21"><a href="#cmnt_ref70" id="cmnt70">[br]</a><span class="c6">TK</span></p></div><div class="c19"><p class="c21"><a href="#cmnt_ref71" id="cmnt71">[bs]</a><span class="c6">TK</span></p></div><div class="c19"><p class="c21"><a href="#cmnt_ref72" id="cmnt72">[bt]</a><span class="c6">TK</span></p></div><div class="c19"><p class="c21"><a href="#cmnt_ref73" id="cmnt73">[bu]</a><span class="c6">TK</span></p></div></body></html>